Order array of objects by date DESC

Shaun Ericson
Kilo Contributor

I have an array of objects:

var array = [ {date: "2018-08-26T00:00:00Z"}, {date: "2020-08-26T00:00:00Z"}, {date: "2017-08-26T00:00:00Z"} ]

...and trying to reorder the objects in the array by "date" DESC.

I found this post which has a great solution in it, and the solution works with the dates provided in the array from that post: Community Post however if you swap out the array of dates for the ones I need to work with I can't get it to work. Here is a snippet of code from what I am trying to do:

var array = [ {date: "2018-08-26T00:00:00Z"}, {date: "2020-08-26T00:00:00Z"}, {date: "2017-08-26T00:00:00Z"} ]

array.sort(function(a, b) {
   var dateA=new Date(a.date), dateB=new Date(b.date)
   return dateA-dateB;
});

gs.print(array[0].date)

If I run this code against a Node server, it works great. 

I have troubleshot this a lot, so providing the simplest version of what I am trying to solve less all the noise and millions of iterations of attempts. 🙂 Anyone have any idea on how to make this logic work with the dates that I am working with?

Thanks in advance!

 

1 ACCEPTED SOLUTION

Simon Christens
Kilo Sage

I dont think "new Date()" takes "2017-08-26T00:00:00Z" as a valid format.

But it should be possible to sort the array by:

var array = [ {date: "2018-08-26T00:00:00Z"}, {date: "2020-08-26T00:00:00Z"}, {date: "2017-08-26T00:00:00Z"} ]

array.sort(function(a, b) {
  
  var date1 = new GlideDateTime();
	date1.setValueUTC(a.date.replace("T", " ").replace("Z", ""), "yyyy-MM-dd HH:mm:ss");
	
	var date2 = new GlideDateTime();
	
	date2.setValueUTC(b.date.replace("T", " ").replace("Z", ""), "yyyy-MM-dd HH:mm:ss");
	
	if (date1.getValue() < date2.getValue())
		return 1;
	
	if (date1.getValue() > date2.getValue())
		return -1;
	
	return 0;
});

View solution in original post

5 REPLIES 5

Allen Andreas
Administrator
Administrator

Hi,

Sorry, I just wanted to double-check, so does the post you linked work or not work for you?

Because you said it works for you, then you said it doesn't work.

So I wasn't sure if you were just looking for alternative method, but you already have a solution, or perhaps misstated that it works for you because it doesn't?

Please mark reply as Helpful/Correct, if applicable. Thanks!


Please consider marking my reply as Helpful and/or Accept Solution, if applicable. Thanks!

Shaun Ericson
Kilo Contributor

Hi @Allen A, thanks for the response and apologies if my post was a bit confusing. The link I provided works for the dates that are in that link's array, but when i substitute the dates in the array for how my dates are from the data I am working with it does not work. 

Hopefully that makes sense, will also update my original post so it is less confusing. 

Thanks. 

Hi Shaun,

considering you only want to compare dates and not time; below script should help you

try to run in background and check once

var str = '[{"date": "2018-08-26T00:00:00Z"}, {"date": "2020-08-26T00:00:00Z"}, {"date": "2017-08-26T00:00:00Z"}]';

var parser = JSON.parse(str);

var array = [];

var length = parser.length;

for(var i=0;i<length;i++){
var value = parser[i].date;
var gdt = new GlideDateTime(value);
array.push(gdt.getDate().toString());
}

var latest = array[0];
for(var i=0;i<array.length;i++){
   if (GlideDateTime(array[i]) > GlideDateTime(latest))
    latest = array[i];
}

gs.info('Latest Date from array is:' + latest);

Mark Correct if this solves your issue and also mark 👍 Helpful if you find my response worthy based on the impact.
Thanks
Ankur

 

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Simon Christens
Kilo Sage

I dont think "new Date()" takes "2017-08-26T00:00:00Z" as a valid format.

But it should be possible to sort the array by:

var array = [ {date: "2018-08-26T00:00:00Z"}, {date: "2020-08-26T00:00:00Z"}, {date: "2017-08-26T00:00:00Z"} ]

array.sort(function(a, b) {
  
  var date1 = new GlideDateTime();
	date1.setValueUTC(a.date.replace("T", " ").replace("Z", ""), "yyyy-MM-dd HH:mm:ss");
	
	var date2 = new GlideDateTime();
	
	date2.setValueUTC(b.date.replace("T", " ").replace("Z", ""), "yyyy-MM-dd HH:mm:ss");
	
	if (date1.getValue() < date2.getValue())
		return 1;
	
	if (date1.getValue() > date2.getValue())
		return -1;
	
	return 0;
});