[英]How to sort the string based on numbers present in it, separated by (.)
[英]How to sort array based on the numbers in string?
给定此数组= [ "3ab1", "2a0", "1abc2" ]
如何将其排序为[ "1abc2", "3ab1", "2a0" ]
(最后一个数字的降序)
并返回[ 1,3,2 ]
。 (每学期的第一个数字)
当最后一个数字和下一个最后一个数字不连续时,返回的值应为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 ]
我正在考虑将数组转换为字符串,替换前面的数字和字母,然后对数组进行排序。 但是我不知道如何将替换的零件放回原来的编号。 这是我对排序后的最终数组返回的尝试。
var ary = [ "1abc2", "3ab1", "2a0" ]; console.log(((ary.toString()).match(/\\d+(?!,)/g)).slice(0, -1));
我在根据数字对数组进行排序时看到了这些问题,但它们似乎对我不起作用。
您的问题有点奇怪,但是您可以使用map
和parseInt
实现此目的:
var arr = [ "1abc2", "3ab1", "2a0" ]; var res = arr.map(function (i) { return parseInt(i); }); console.log(res);
排序和映射的组合应该可以解决问题。
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);
下面的脚本首先根据结束编号对降序排列的数组进行排序,
然后仅返回已排序数组的起始编号。
(我更改了数组中的数字,以显示它们可以长于一位数字。)
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
您可以在使用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
在最新的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.