0

I am having Nodejs model,

itemModel.find({deleted:false},function(error,response){

  });

// here i am getting response,

[{_id:'3221xxx',status:"b"},
 {_id:'3221xxx',status:"a"},
 {_id:'3221xxx',status:"d"},
 {_id:'3221xxx',status:"c"}
]

// I have rule in array e.g: ['a','b','c','d'] in this order, i have to list the data based on this array. like this,

[{_id:'3221xxx',status:"a"},
 {_id:'3221xxx',status:"b"},
 {_id:'3221xxx',status:"c"},
 {_id:'3221xxx',status:"d"}
]

Please help me the better & easy way

ramamoorthy_villi
  • 1,939
  • 1
  • 17
  • 40
  • use [sort](http://docs.mongodb.org/manual/reference/operator/meta/orderby/) like this `find().sort({"status":-1})` – Neo-coder Jul 14 '15 at 08:08
  • if the rule change in that array means this doesnt work ..eg. ['d',a'','c','d'].. this will not work – ramamoorthy_villi Jul 14 '15 at 08:10
  • possible duplicate of [Does MongoDB's $in clause guarantee order?](http://stackoverflow.com/questions/22797768/does-mongodbs-in-clause-guarantee-order) – Blakes Seven Jul 14 '15 at 08:16
  • 1
    use underscore: `_.sortBy(result,function(item){return ['c','b','a','d'].indexOf(item.status) })` – hassansin Jul 14 '15 at 08:46

0 Answers0