2

Currently I am using this code to Sort the data within nodejs code.

const popularPlaces = await Places
.find({location: "New York"})
.sort( { locationscore: -1 } )

The code above finds location New York and sorts popular places with higher locationscore and dumps into popularPlaces. This way I can use it on pug/jade template like this:

each value in popularPlaces
  .h2 Top Popular Places #{value.placename}

But I want to do the sorting in pug/jade itself... and not by nodejs.

This is the JSON that gets returned by popularPlaces

[{
    "placename": "Modern Stop", 
    "locationscore": 0.8734, 
},{
    "placename": "Next Coffee", 
    "locationscore": 0.807121, 
},{
    "placename": "Mahattan Bakery", 
    "locationscore": 0.899802, 
},{
    "placename": "Mainland Cafe", 
    "locationscore": 0.801271, 
},{
    "placename": "Combo Boo", 
    "locationscore": 0.808973, 
},{
    "placename": "Baker's Bakery", 
    "locationscore": 0.8123, 
}] 

How can I do that?

floss
  • 2,603
  • 2
  • 20
  • 37

1 Answers1

2

Try

- popularPlaces.sort(function(a, b){ return (a.locationscore > b.locationscore) ? 1 : ((b.locationscore > a.locationscore) ? -1 : 0); })

each value in popularPlaces
  .h2 Top Popular Places #{value.placename}
Sandeep Ranjan
  • 824
  • 15
  • 33