Getting alphabetical order from pouchDB query

This is what my code looks like:

var physicianList = [];

function map(doc){
    if(doc.PhysicianKey && doc.PhysicianName){
        console.log(doc.PhysicianName);
        emit(doc.PhysicianKey, doc.PhysicianName);
    }
}

dbPhysicianList.query({map: map}, function(error, response){ 
    if(!error){
        angular.forEach(response.rows, function(physician){
            physicianList.push(physician);
        })
        deferred.resolve(physicianList);
    }else{
        console.log(error);
        deferred.reject();
    }}, {include_docs: true});


    return deferred.promise;
}

      

As you can see, in my map function, I log out doc.PhysicianName

, and when I do, they log out in alphabetical order. However, they end up in the array physicianList

in alphabetical order, I think in the order of input. I would like the final result to be ordered alphabetically in the physicianList array.

For what it's worth, I'm using PouchDB, but I believe the query should work exactly like CouchDB.

+3


source to share


1 answer


I didn't realize that the order of your variables emit()

is the ordering of the result set (I'm a beginner).

I ended up doing this:



function map(doc){
    emit({physicianName: doc.PhysicianName, physicianKey: doc.PhysicianKey} );
}

      

And it worked exactly as desired (alphabetically ordered by doc.physicianName

).

+2


source







All Articles