2

目前我正在使用此代码对 nodejs 代码中的数据进行排序。

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

上面的代码查找位置New York并对热门地点进行排序,locationscore然后转储到popularPlaces. 这样我可以在这样的pug/jade模板上使用它:

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

但我想自己进行排序pug/jade......而不是通过nodejs。

这是由返回的 JSONpopularPlaces

[{
    "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, 
}] 

我怎样才能做到这一点?

4

1 回答 1

2

尝试

- 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}
于 2018-01-20T04:20:43.413 回答