How to organize the result using a reduced value? Couchdb

Can I order the result of a view from the value returned by the abbreviated?

{ "rows": [ {"key":"bob","value":2}, {"key":"john","value":3}, {"key":"zztop","value":1} ] } 

I want to get this result:

 { "rows": [ {"key":"zztop","value":1}, {"key":"bob","value":2}, {"key":"john","value":3} ] } 
+6
javascript couchdb
source share
1 answer

do you just want to sort the rows array by value property for each object? you can specify your own comparison method for js sort method.

 myResult.rows.sort(function (a, b) { return parseInt(a.value,10) - parseInt(b.value,10); }); 

parameters a and b will be a record from the rows array. The method returns an int value greater than zero, 0 or less than zero. greater than 0 means that a must be ordered before b , the opposite value is less than 0, and 0 means that they are equal.

0
source share

All Articles