React 按值排序列表,然后按字母顺序排序

时间:2021-02-09 11:04:09

标签: reactjs list sorting

我首先尝试按项目的值(存储在 Firebase 中)对项目列表进行排序(具有相同值的项目具有相同的颜色),然后按字母顺序(因此绿色项目应按字母顺序排列,黄色项目应按字母顺序排列)也..)。我尝试使用 ||还有 a.localeCompare(b),但似乎没有任何效果。它可以是按字母顺序或按值(随机),但两者都不是。

enter image description here

这是我按值排序的代码:

.tabcontent:not(:target) {
  display: none;
}

.tab-container a {
  display: inline-block;
  border: 1px currentColor solid;
  border-radius: 3px;
  background: #e8e8e8;
  padding: .15em .5em;
  text-decoration: none;
  color: #000;
  font: .8em Arial;
}

1 个答案:

答案 0 :(得分:0)

我想出了这个,而且效果很好!

.sort((a, b) =>
 a.daysLeftForNextPurchase > b.daysLeftForNextPurchase
 ? 1
 : a.daysLeftForNextPurchase === b.daysLeftForNextPurchase
 ? a.shoppingListItemName > b.shoppingListItemName
 ? 1
 : -1
 : -1,
)