首页 > 编程语言 > 详细

[Javascript] Sort by multi factors

时间:2019-12-02 00:28:26      阅读:73      评论:0      收藏:0      [点我收藏+]

For example, we have a 2D arrays;

const arys = [
  [abw, 3],
  [bcd, 2],
  [dcw, 2]
];

 

We want to sort by the number first, if the number are the same, then we want to sort by name DESC:

So the result should be:

[ [ abw, 3 ], [ dcw, 2 ], [ bcd, 2 ] ]

 

const result = arys.sort((a, b) => {
  if (b[1] > a[1]) {
    return true
  } else if (b[1]  < a[1]) {
    return false;
  } else {
    return b[0].toLowerCase().localeCompare(a[0].toLowerCase())
  }
});

 

[Javascript] Sort by multi factors

原文:https://www.cnblogs.com/Answer1215/p/11968201.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!