趋势按观看次数和日期排序

时间:2019-11-12 19:58:48

标签: javascript arrays algorithm sorting javascript-objects

我正在尝试通过一些自定义更改来实现Hacker News ranking algorithm。我需要按受欢迎程度/趋势对帖子(对象数组)进行排序。简而言之,我想比较观看次数x发布日期并获取热门帖子。

我尝试了以下代码:

const array = [
  { title: "1", views: 100, publishDate: 1 }, // publish date = 1 hour ago
  { title: "2", views: 400, publishDate: 2 }, // publish date = 2 hour ago
  { title: "3", views: 300, publishDate: 3} // publish date = 3 hour ago
];


const sortByTrending = array.sort((a, b) => b.views / Math.pow(b.publishDate, 1.8) - a.views / 
Math.pow(a.publishDate, 1.8));

console.log(sortByTrending);

// Output:
[
  {
    title:"1",
    views:300,
    publishDate:1
  },{
    title:"3",
    views:1000,
    publishDate:3
  },{
    title:"2",
    views:400,
    publishDate:2
  }
];

它正在以某种方式工作。但是,有没有更好的方法来获得更准确的结果呢?

0 个答案:

没有答案