Couchbase View : Sorting on Keys

I am using Couchbase 3.0 and I have a use case where I want to
perform sorting, but as I came through couchbase does not provide Sorting
on Values but it provides Sorting on Keys, But as I use
descending(true) it is returning me empty list. And on the other hand If
I just simply use it without descending then it is giving me all the
docs related.

My Map function is :

function (doc, meta) {
  emit([meta.id,doc.latest.sortData],null);
}
}

Now my use case is that I want to perform a match query on meta.id
and then for all the matched cases sort the data and then find out the
top values.

The code that i am using to so is :

ViewQuery.from(DesignDocName, viewName).startKey(JsonArray.from(write(List("something","")))).descending(true).limit(5).stale(Stale.FALSE))

If I remove the descending parameter then I get the related rows but
they are not sorted. So could you please provide me a way in which this
can be done.

Any Help is appreciated

Thanks!

Hey @shiv4nsh,

I’ve responded with a comment on your Stack Overflow version of this post:

To keep things in sync, can you please provide us with some sample data and the expected results. We think you may need to use a map and a reduce in order to accomplish this so please provide a value to your emit function as well.

Best,

I solved this issue by adding the sorting value appropriately to the key. It might not be the best solution but I guess its working.

My use case was : I want to sort the users on the basis of the profit that they earn and profit is the sorted value here, and the meta.id was the users id.