简体   繁体   English

JavaScript 按字符串中的数字排序

[英]JavaScript Sort by number in a string

How could i sort eg an array like this one:我怎么能像这样对数组进行排序:

[
  '<@520968882077433856> is level **7,64** _(18115.5 total xp)_',
  '<@289037609274048513> is level **7,07** _(14473 total xp)_',
  '<@672835870080106509> is level **8,25** _(22473.5 total xp)_',
  '<@536686935134175254> is level **6,22** _(10184.5 total xp)_',
]

I would like to sort it by the amount of total xp, so the number within the brackets.我想按总 xp 的数量对其进行排序,因此括号内的数字。

You can use the sort callback.您可以使用排序回调。 Extract the number with a regular expression:使用正则表达式提取数字:

 const data = [ '<@520968882077433856> is level **7,64** _(18115.5 total xp)_', '<@289037609274048513> is level **7,07** _(14473 total xp)_', '<@672835870080106509> is level **8,25** _(22473.5 total xp)_', '<@536686935134175254> is level **6,22** _(10184.5 total xp)_', ]; const regex = /(?<=_\()[\d.]+/; data.sort((a, b) => +a.match(regex) - +b.match(regex)); console.log(data);

The + is used to convert the returned array (with one match element) to a string and then that to a number. +用于将返回的数组(具有一个匹配元素)转换为字符串,然后再将其转换为数字。 You could use ?.[0]你可以使用?.[0] to actually get that first array element.实际获取第一个数组元素。

We can try sorting using a custom lambda expression:我们可以尝试使用自定义 lambda 表达式进行排序:

 var input = [ '<@520968882077433856> is level **7,64** _(18115.5 total xp)_', '<@289037609274048513> is level **7,07** _(14473 total xp)_', '<@672835870080106509> is level **8,25** _(22473.5 total xp)_', '<@536686935134175254> is level **6,22** _(10184.5 total xp)_', ]; var output = input.sort( (a, b) => parseFloat(a.match(/(\d+(?:\.\d+)?) total xp\b/)[1]) - parseFloat(b.match(/(\d+(?:\.\d+)?) total xp\b/)[1])); console.log(output);

In the above we are extracting the number which is followed by total xp , parsing it as a float, and then using that value in the sorting comparator.在上面,我们提取了total xp后面的数字,将其解析为浮点数,然后在排序比较器中使用该值。 If you want descending order, then just reverse the inequality in the lambda function.如果要降序,那么只要将lambda function中的不等式反转即可。

Here is another try, looking for a space after the number:这是另一种尝试,在数字后寻找空格:

 const arr=[ '<@520968882077433856> is level **7,64** _(18115.5 total xp)_', '<@289037609274048513> is level **7,07** _(14473 total xp)_', '<@672835870080106509> is level **8,25** _(22473.5 total xp)_', '<@536686935134175254> is level **6,22** _(10184.5 total xp)_', ]; function getN(s){ return s.match(/_\((.+?) /)[1]; } arr.sort((a,b)=>getN(a)-getN(b)) console.log(arr)

Please try this.请试试这个。

 const array = [ "<@520968882077433856> is level **7,64** _(18115.5 total xp)_", "<@289037609274048513> is level **7,07** _(14473 total xp)_", "<@672835870080106509> is level **8,25** _(22473.5 total xp)_", "<@536686935134175254> is level **6,22** _(10184.5 total xp)_", ]; const extractDigitNumber = (string) => { return string.match(/(?<=\_\()(.*?)(?=\)\_)/)[0].replace(/[^\d\.]+/g, '') } array.sort((a, b) => { return extractDigitNumber(a) - extractDigitNumber(b) }) console.log(array)

Details are commented in example详细信息在示例中进行了注释

 let data = [ '<@520968882077433856> is level **7,64** _(18115.5 total xp)_', '<@289037609274048513> is level **7,07** _(14473 total xp)_', '<@672835870080106509> is level **8,25** _(22473.5 total xp)_', '<@536686935134175254> is level **6,22** _(10184.5 total xp)_', ] /* * -.map() through array * - on every iteration run.match() using this regex: * /(\d+\.?\d*)(?=\stotal)/ which means: "match one or more numbers, then a possible dot, and then possible numbers, but only if it's followed by a space and literal 'total'" * - return a match in an array and the current index * -.sort() the sub-arrays by their first index (sub[0]) */ let indices = data.map((str, idx) => [+str.match(/(\d+\.?\d*)(?=\stotal)/g), idx]).sort((a, b) => a[0] - b[0]); console.log(JSON.stringify(indices)); //Define an empty array let result = []; /* * - run new array of arrays with.forEach() * - get the string within the original array (data) by the current sub-array's second index (sub[1]) * - push the value into the empty array (result) */ indices.forEach((sub, idx) => result.push(data[sub[1]])); console.log(result);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM