Array Date Sort is not working

sainath3
Mega Guru

Hi Team,

 

Am unable to sort the date.

I have tried:

var arr=['2010-07-12', '2020-07-24'];
arr.sort();
gs.info(arr);

but output is: 

*** Script: 2010-07-12,2020-07-24

Suggest me how to sort the dates.

Requirement is to fetch the latest date.

1 ACCEPTED SOLUTION

wndydngy
Tera Expert

Hi,

 

I think your code is working as expected. It sorted your array in an ascending order. If you want to have the array sorted in descending order, you can check the code below.

 

var arr=['2010-07-12', '2020-07-24'];
arr.sort().reverse();
gs.info(arr);

 

Let me know if this helps. Thanks!

View solution in original post

3 REPLIES 3

wndydngy
Tera Expert

Hi,

 

I think your code is working as expected. It sorted your array in an ascending order. If you want to have the array sorted in descending order, you can check the code below.

 

var arr=['2010-07-12', '2020-07-24'];
arr.sort().reverse();
gs.info(arr);

 

Let me know if this helps. Thanks!

Thank You for quick reply.

Its working fine.

It was silly mistake.😆

Manmohan K
Tera Sage

Hi @sainath3 ,

 

Please refer below article for code that can be used to sort dates in an array 

https://www.servicenow.com/community/itsm-forum/sorting-dates-in-an-array/m-p/913137