I have array of strings
sample0
sample1
sample11
sample12
sample13
sample14
sample2
sample21
sample3
But i need in this way. I am not able to figure out the solution. And Prefix may not be sample all the time.
sample0
sample1
sample2
sample3
sample11
sample12
sample13
sample14
sample21
Use Regular expressions /\\d+$/
to match only the number presented at the end in the string with Array's sort()
like the following:
var strArr = ['sample0', 'sample1', 'sample11', 'sample12', 'sample13', 'sample14', 'sample2', 'sample21', 'sample3']; var strRes = strArr.sort(function(a, b){ return a.match(/\\d+$/) - b.match(/\\d+$/); }) console.log(strRes);
Note: This will extract number only from the end and will sort according to that.
Solution-1:
var arr = ['sample0', 'sample1', 'sample11', 'sample12', 'sample13', 'sample14', 'sample2', 'sample21', 'sample3']
arr.sort(function (a, b) {
return a.localeCompare(b, undefined, { numeric: true, sensitivity: 'base' });
});
console.log(arr);
The localeCompare() method returns a number indicating whether a reference string comes before or after or is the same as the given string in sort order.
Solution-2:
function naturalCompare(a, b) {
var ax = [], bx = [];
a.replace(/(\d+)|(\D+)/g, function(_, $1, $2) { ax.push([$1 || Infinity, $2 || ""]) });
b.replace(/(\d+)|(\D+)/g, function(_, $1, $2) { bx.push([$1 || Infinity, $2 || ""]) });
while(ax.length && bx.length) {
var an = ax.shift();
var bn = bx.shift();
var nn = (an[0] - bn[0]) || an[1].localeCompare(bn[1]);
if(nn) return nn;
}
return ax.length - bx.length;
}
arr.sort(naturalCompare);
console.log(arr);
This solution has taken from https://stackoverflow.com/a/15479354/3910232
If the prefix "sample" is constant then
var numString=['sample1','sample12','sample123','sample2','sample0','sample23'];
var num=new Array(numString.length);
for (var i = 0; i < numString.length; i++) {
num[i]=numString[i].substring(6);
}
var st=numString[0].substring(0,6);
num.sort();
var ne=(st + num.join(';' + st)).split(';');
alert(ne);
The smart-sort package can accomplish this. I'm sure there are other solutions. Look for the keywords "natural sorting" and "smart sorting".
var collator = new Intl.Collator(undefined, {numeric: true, sensitivity: 'base'});
var myArray = ['sample1', 'sample12', 'sample3'];
myArray.sort(collator.compare);
Try this out.
var array = ['sample0','sample1','sample11','sample12','sample13', 'sample14','sample2','sample21','sample3'] var sortedArray = array.sort(function(a, b){ var regXStr = /[^a-zA-Z]/g, regXNum = /[^0-9]/g; var aStr = a.replace(regXStr, "").toLowerCase(); var bStr = b.replace(regXStr, "").toLowerCase(); if(aStr === bStr) { var aNum = parseInt(a.replace(regXNum, ""), 10); var bNum = parseInt(b.replace(regXNum, ""), 10); return aNum === bNum ? 0 : aNum > bNum ? 1 : -1; } else { return aStr > bStr ? 1 : -1; } }); console.log(sortedArray)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.