Sort mixed alpha/numeric Array in javascript

杀马特。学长 韩版系。学妹 提交于 2021-01-28 07:02:56

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!