问题
I have a mixed array that I need to sort by digit and then by alphabet
var x = ['1','2A','2B','2AA','2','10A','10','11','12A','12B','12']
Desired Output
sortedArray = ['1','2','2A','2B','2AA','10','10A','11','12','12A','12B']
I had tried using lodash but wasn't getting desired result
var x = ['1','2A','2B','2AA','2','10A','10','11','12A','12B','12']
_.sortBy(x);
//lodash result
["1", "10", "10A", "11", "12", "12A", "12B", "2", "2A", "2AA", "2B"]
回答1:
You can use parseInt to get the number part and sort
it. If both a
and b
have the same number, then sort them based their length
. If they both have the same length, then sort them alphabetically using localeCompare
let array = ['1','2A','2B','2AA','2','10A','10','11','12A','12B','12'];
array.sort((a, b) => parseInt(a) - parseInt(b)
|| a.length - b.length
|| a.localeCompare(b));
console.log(array)
回答2:
You can use custom sot function, within custom function split digit and non-digit seperately and sort based on num and if both are equal compare the non-digit part.
const arr = ['1', '2A', '2B', '2AA', '2', '10A', '10', '11', '12A', '12B', '12']
arr.sort((a, b) => {
// extract digit and non-digit part from string
let as = a.match(/(\d+)(\D*)/);
let bs = b.match(/(\d+)(\D*)/);
// either return digit differennce(for number based sorting)
// in addition to that check string length(in case digits are same)
// or compare non-digit part(string comparison)
return as[1] - bs[1] || a.length - b.length ||as[2].localeCompare(bs[2]);
})
console.log(arr)
来源:https://stackoverflow.com/questions/57625059/sort-mixed-alpha-numeric-array-in-javascript