Skip to content
Advertisement

Sort the array of database object-id

I have a array of mongodb object-id and i want to sort them in such a way that similar IDs are next to each other

example :

Input :

var array = ["507f191e810c19729de860ea","00000020f51bb4362eee2a4d",” 507f191e810c19729de860ea”]

Output :

var array = ["507f191e810c19729de860ea","507f191e810c19729de860ea","00000020f51bb4362eee2a4d"]

Advertisement

Answer

Simply sorting the input array would result in the same input array being sorted in natural order (converting the input to a string, if needed, and comparing the elements UTF-16 code units):

array.sort();

In case you would want the reverse order, you can reverse the array:

array.sort().reverse();
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement