Sorting in distinct data

595 Views Asked by At

on calling db.distinct() method always return the data in ascending order sorted. Is their a way I can get the data sorted in descending order?

1

There are 1 best solutions below

1
On

The distinct command returns a javascript array, so you can always sort it and reverse it:

var array = db.coll.distinct("{field_name}")
array.sort()
array.reverse()

If using java driver, you can do something such as:

List list = coll.distinct("{field_name}");
Collections.sort(list);
Collections.reverse(list);