简体   繁体   English

如何根据字符串中的数字对数组进行排序?

[英]How to sort array based on the numbers in string?

Given this array = [ "3ab1", "2a0", "1abc2" ] 给定此数组= [ "3ab1", "2a0", "1abc2" ]

How do I sort it to [ "1abc2", "3ab1", "2a0" ] (descending order of the last number) 如何将其排序为[ "1abc2", "3ab1", "2a0" ] (最后一个数字的降序)

and return [ 1,3,2 ] . 并返回[ 1,3,2 ] (the first numbers of each term) (每学期的第一个数字)

When the last number and the next last number is not consecutive, the value returned should be 0. 当最后一个数字和下一个最后一个数字不连续时,返回的值应为0。

[ "2x2", "3x0", "2x1" ] ==> [ 2, 2, 3 ]

[ "22x0", "3x9", "2x1" ] ==> [ 3, 0, 0, 0, 0, 0, 0, 0, 2, 22 ]

[ "2x4", "3x0" ] ==> [ 2, 0, 0, 3 ]

[ "axn", "bx(n-2)" ] ==> [ "axn", "0x(n-1)", bx(n-2) ] ==> [ a, 0, b ]

I was thinking of converting to the array to string, replacing the number and letters in front and then sorting the array. 我正在考虑将数组转换为字符串,替换前面的数字和字母,然后对数组进行排序。 But I do not know how put the part that was replaced back to its original number. 但是我不知道如何将替换的零件放回原来的编号。 This is my attempt on returning the final array once it is sorted. 这是我对排序后的最终数组返回的尝试。

 var ary = [ "1abc2", "3ab1", "2a0" ]; console.log(((ary.toString()).match(/\\d+(?!,)/g)).slice(0, -1)); 

I saw these questions on sorting arrays based on numbers but they do not seem to work for me. 我在根据数字对数组进行排序时看到了这些问题,但它们似乎对我不起作用。

How to sort an array of integers correctly 如何正确排序整数数组

Sort Array Elements (string with numbers), natural sort 排序数组元素(带数字的字符串),自然排序

Your question is a bit odd, but you can achieve this using map and parseInt : 您的问题有点奇怪,但是您可以使用mapparseInt实现此目的:

 var arr = [ "1abc2", "3ab1", "2a0" ]; var res = arr.map(function (i) { return parseInt(i); }); console.log(res); 

A combination of sort and map should do the trick. 排序和映射的组合应该可以解决问题。

  const ary = [ "1abc2", "3ab1", "2a0" ]; const newarray = ary .sort((a, b) => { return a[a.length - 1] < b[b.length - 1]; }) .map((a) => { return parseInt(a[0]); }); console.log(newarray); 

The script below first sorts the array descending, based on the end-numbers, 下面的脚本首先根据结束编号对降序排列的数组进行排序,
and then returns only the start-numbers of the sorted array. 然后仅返回已排序数组的起始编号。

(I changed your numbers in the array to show that they can be longer than one digit.) (我更改了数组中的数字,以显示它们可以长于一位数字。)

 var array = ["31ab12", "20a40", "11abc27"]; array.sort(function(a,b) { function getEndNum(str) {for (var i=str.length-1; i>=0; --i) {if (isNaN(str.charAt(i))) {return str.substring(i+1);}}} //this function returns the end-number of the supplied string return getEndNum(b) - getEndNum(a); //sort array descendingly, based on the end-numbers }); console.log(array.map(function(a){return parseInt(a);})); //create a new array with only the start-numbers 
jsfiddle: https://jsfiddle.net/nu8vf837/ jsfiddle: https ://jsfiddle.net/nu8vf837/

You can use regular expression to get the numbers when using sort and reduce to get the result: 您可以在使用sort时使用正则表达式获取数字,并通过reduce来获取结果:

 var array = [ "22x0", "3x9", "2x1" ]; var reS = /^\\d+/, // regexp for getting all digits at the start of the string reE = /\\d+$/; // regexp for getting all digits at the end of the string var result = array.sort(function(a, b) { // First: sort the array a = reE.exec(a); // get the last number from the string a b = reE.exec(b); // get the last number from the string b return b - a; // sort in a descending order }).reduce(function(res, str, i) { // Then: accumulate the result array var gap = reE.exec(array[i - 1]) - reE.exec(str); // calculate the gap between this string str and the last string array[i - 1] (gap = N_of_last_string - N_of_this_string) if(gap > 0) // if there is a gap while(--gap) res.push(0); // then fill it with 0s res.push(+reS.exec(str)); // push this string number return res; }, []); console.log("Sorted array:", array); // array is now sorted console.log("Result:", result); // result contain the numbers 

In recent ECMAScript versions you can do it shortly using arrow functions like this: 在最新的ECMAScript版本中,您可以使用以下箭头功能很快完成此操作:

 let array = [ "22x0", "3x9", "2x1" ]; let reS = /^\\d+/, reE = /\\d+$/; let result = array.sort((a, b) => reE.exec(b) - reE.exec(a)) .reduce((res, str, i) => { let gap = reE.exec(array[i - 1]) - reE.exec(str); if(gap > 0) while(--gap) res.push(0); res.push(+reS.exec(str)); return res; }, []); console.log("Sorted array:", array); console.log("Result:", result); 

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

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