Home > Software design >  Sort the array of database object-id
Sort the array of database object-id

Time:08-13

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"]

CodePudding user response:

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();
  • Related