Search code examples
sails.jssails-mongo

How to sort records using object's key in sails js


Hello friends I am writing the web service using sails js. I am fetching all posts and getting the following response :

[
  {
     id: "559458c51ccc9c716dabf666",
     comments : [],
     liked : {
         data : [
            { 
              id: "559458c51eee9c716dabf666",
              username : "abc"
            },
            { 
              id: "559458c51eee9c716dabf111",
              username : "xyz"
            }
         ],
         count : 2
     }
  },

  {
     id: "559458c51ccc9c716dabf666",
     comments : [],
     liked : {
         data : [
            { 
              id: "559458c51eee9c716dabf666",
              username : "abc"
            },
            { 
              id: "559458c51eee9c716dabf666",
              username : "pqr"
            },
            { 
              id: "559458c51eee9c716dabf111",
              username : "xyz"
            }
         ],
         count : 3
     }
  }
]

I want to sort above records using the count of liked posts. In above response we are getting liked count as liked { data : [], count : 2}.

I am doing like this :

getPost: function(callback) {
        Posts.find().sort('liked.count desc').populateAll().exec( function (err, posts) {
            if(err) {
                return callback({error:err, code:500});
            }
            if (posts) {
                callback(null,posts);
            }

        });
    }

What to do to sort the posts using the count which is in the liked : {}


Solution

  • I have removed sort('liked.count desc') from getPost.

    getPost: function(callback) {
            Posts.find().populateAll().exec( function (err, posts) {
                if(err) {
                    return callback({error:err, code:500});
                }
                if (posts) {
                    callback(null,posts);
                }
    
        });
    }
    

    And In controller I was calling above getPost like below :

    getTopTip : function (req,res) {
            Posts.getPost(function (err,tips) {
                if(err) {
                    return res.notFound();
                }
    
                // console.log(tips);
                res.json({'count':tips.length, 'data':tips});
    
            });
        }
    

    I checked response using console.log and I m getting response like below :

    [
      {
         id: "559458c51ccc9c716dabf666",
         comments : [],
         likes : [
                { 
                  id: "559458c51eee9c716dabf666",
                  username : "abc"
                },
                { 
                  id: "559458c51eee9c716dabf111",
                  username : "xyz"
                }
             ]
      },
    
      {
         id: "559458c51ccc9c716dabf666",
         comments : [],
         likes :  [
                { 
                  id: "559458c51eee9c716dabf666",
                  username : "abc"
                },
                { 
                  id: "559458c51eee9c716dabf666",
                  username : "pqr"
                },
                { 
                  id: "559458c51eee9c716dabf111",
                  username : "xyz"
                }
             ]
      }
    ]
    

    I write a sort function in that controller where I am calling getPost. That function look like below :

    function sortByKey(array, key) {
        return array.sort(function(a, b) {
            var x = a[key]; var y = b[key];
            return ((x > y) ? -1 : ((x < y) ? 1 : 0));
        });
    } 
    

    And In getTopTip I called this sort function like below :

    getTopTip : function (req,res) {
            Posts.getPost(function (err,tips) {
                if(err) {
                    return res.notFound();
                }
                if(tips) {
                    tips = sortByKey(tips, 'likes')
                    res.json({'count':tips.length, 'data':tips});
                }
            });
        }
    

    And it works. I have got Posts in descending order.