如何按降序对具有多个属性的对象数组进行排序[重复]

问题描述 投票:0回答:1

嗨,我有3个复选框MostCommented,MostRead,MostLiked,默认情况下,所有这些都将被单击。通过检查,我应该能够过滤我的数组。我的数组在下面。

array = [{"LikeCount:2,commentCount:4,ReadCount:6"},
         {"LikeCount:1,commentCount:1,ReadCount:3"},
         {"LikeCount:4,commentCount:2,ReadCount:3"},
         {"LikeCount:4,commentCount:1,ReadCount:1"},
         {"LikeCount:4,commentCount:2,ReadCount:5"}, 
         {"LikeCount:4,commentCount:3,ReadCount:1"},
         {"LikeCount:5,commentCount:3,ReadCount:5"}]

我需要这样的输出。

array = [{"LikeCount:5,commentCount:3,ReadCount:5"},
         {"LikeCount:4,commentCount:1,ReadCount:3"},
         {"LikeCount:4,commentCount:2,ReadCount:3"},
         {"LikeCount:4,commentCount:2,ReadCount:3"},
         {"LikeCount:4,commentCount:3,ReadCount:5"},
         {"LikeCount:2,commentCount:4,ReadCount:6"},
         {"LikeCount:1,commentCount:1,ReadCount:3"}]

任何输入都会有帮助。

javascript reactjs spfx
1个回答
-1
投票

您的预期输出显示您需要根据LikeCount进行排序,所以>

尝试一下:

  let array = [{ LikeCount: 2, commentCount: 4, ReadCount: 6 },
  { LikeCount: 1, commentCount: 1, ReadCount: 3 },
  { LikeCount: 4, commentCount: 2, ReadCount: 3 },
  { LikeCount: 4, commentCount: 1, ReadCount: 1 },
  { LikeCount: 4, commentCount: 2, ReadCount: 5 },
  { LikeCount: 4, commentCount: 3, ReadCount: 1 },
  { LikeCount: 5, commentCount: 3, ReadCount: 5 }]
  array.sort(function (a, b) {
    return b.LikeCount - a.LikeCount
  })
  console.log(array);
© www.soinside.com 2019 - 2024. All rights reserved.