[Javascript] Sort by multi factors

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())
  }
});
原文地址:https://www.cnblogs.com/Answer1215/p/11968201.html