如何按数字字符串和特殊字符对对象数组进行排序

How to sort array of object by string of numbers and special characters

我有一些对象的数组,我想按 dimension 属性 对它们进行排序,其中 属性 是一些数字间隔和特殊字符的字符串。这是我所拥有的例子

let data = [
  {
    "soldTickets": 206,
    "soldRevenue": 12825.309997558594,
    "playedOffTickets": 2915,
    "playedOffRevenue": 495923.22019958496,
    "dimension": "10 - 25",
    "ticketsChange": -2709,
    "revenueChange": -483097.91020202637
  },
  {
    "soldTickets": 172,
    "soldRevenue": 17174.29998779297,
    "playedOffTickets": 2485,
    "playedOffRevenue": 467017.27951049805,
    "dimension": "25 - 50",
    "ticketsChange": -2313,
    "revenueChange": -449842.9795227051
  },
  {
    "soldTickets": 122,
    "soldRevenue": 9892.249984741211,
    "playedOffTickets": 9121,
    "playedOffRevenue": 1129196.3203125,
    "dimension": "> 200",
    "ticketsChange": -8999,
    "revenueChange": -1119304.0703277588
  },
  {
    "soldTickets": 52,
    "soldRevenue": 3159.239990234375,
    "playedOffTickets": 544,
    "playedOffRevenue": 88893.0400390625,
    "dimension": "0 - 10",
    "ticketsChange": -492,
    "revenueChange": -85733.80004882812
  },
  {
    "soldTickets": 38,
    "soldRevenue": 3162.1099700927734,
    "playedOffTickets": 476,
    "playedOffRevenue": 92432.79023742676,
    "dimension": "100 - 200",
    "ticketsChange": -438,
    "revenueChange": -89270.68026733398
  },
  {
    "soldTickets": 37,
    "soldRevenue": 3233,
    "playedOffTickets": 590,
    "playedOffRevenue": 97645.46026611328,
    "dimension": "50 - 100",
    "ticketsChange": -553,
    "revenueChange": -94412.46026611328
  }
];

const toNumber = (str) => {
  if (Number(str)) {
    return Number(str);
  } else {
    return Number(str.substring(0, str.length - 1));
  }
};

   data = data.sort(
        (a, b) =>
          toNumber(String(a["dimension"]).split(' ')[0]) -
          toNumber(String(b["dimension"]).split(' ')[0])
      );
      
  console.log(data)

问题是数组没有按照我想要的方式排序,因为在这种情况下 > 200 应该是最后一个而不是第一个。想要的顺序应该是这样的

['0 - 10', '10 - 25', '25 - 50', '50 - 100', '100 - 200', '> 200']

我做错了什么?提前致谢!

如果我们可以假设第一个数字是我们想要排序的,您可以使用一点正则表达式来找到第一个数字,然后我们可以对其进行排序。

当然,如果您的情况变得更复杂,例如 < 20 等,那么您将需要 re-evaluate 如何决定排序。

例如

let data=[{soldTickets:206,soldRevenue:12825.309997558594,playedOffTickets:2915,playedOffRevenue:495923.22019958496,dimension:"10 - 25",ticketsChange:-2709,revenueChange:-483097.91020202637},{soldTickets:172,soldRevenue:17174.29998779297,playedOffTickets:2485,playedOffRevenue:467017.27951049805,dimension:"25 - 50",ticketsChange:-2313,revenueChange:-449842.9795227051},{soldTickets:122,soldRevenue:9892.249984741211,playedOffTickets:9121,playedOffRevenue:1129196.3203125,dimension:"> 200",ticketsChange:-8999,revenueChange:-1119304.0703277588},{soldTickets:52,soldRevenue:3159.239990234375,playedOffTickets:544,playedOffRevenue:88893.0400390625,dimension:"0 - 10",ticketsChange:-492,revenueChange:-85733.80004882812},{soldTickets:38,soldRevenue:3162.1099700927734,playedOffTickets:476,playedOffRevenue:92432.79023742676,dimension:"100 - 200",ticketsChange:-438,revenueChange:-89270.68026733398},{soldTickets:37,soldRevenue:3233,playedOffTickets:590,playedOffRevenue:97645.46026611328,dimension:"50 - 100",ticketsChange:-553,revenueChange:-94412.46026611328}];

data = data.sort(
  (a, b) =>
    (a.dimension.match(/\d+/)[0] | 0) -
    (b.dimension.match(/\d+/)[0] | 0)
);
      
console.log(data)

ps。 val | 0 只是将字符串转换为数字的一种快速方法,类似于 Number(val)parseInt(val, 10) ,如果我们不转换为数字,您将得到 200 小于 30 等等