Wiz
Wiz

Reputation: 44

In couchdb How to get sorting on value with reduce _count

"myview": {
       "map": "function(doc) { if (doc.type == 'user')  emit(doc.city,null)}",
       "reduce": "_count"
   },

It return city with count in value. Using group=true

How to sort this data base on highest value? Want to get most popular city first...

Upvotes: 0

Views: 242

Answers (1)

Jitender Pal Singh
Jitender Pal Singh

Reputation: 48

You can use list to sort your resultset. Following function can sort anytype of a resultset on value.

"sort": "function() {
        var row;  
        var rows=[]; 
        while(row = getRow()) 
        {  
            rows.push(row) 
        } 

        rows.sort(function(a, b) 
        { 
             return a.value - b.value;
        }); 
        rows.reverse();
        send(JSON.stringify({\"rows\" : rows})) 
    }"

Upvotes: 1

Related Questions