- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-02-2023 12:53 AM
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.
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-02-2023 01:51 AM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-02-2023 01:51 AM
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!
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-02-2023 07:43 AM
Thank You for quick reply.
Its working fine.
It was silly mistake.😆
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
06-02-2023 01:51 AM
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