1

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"]
Snivio
  • 1,741
  • 1
  • 17
  • 25

2 Answers2

3

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)
adiga
  • 34,372
  • 9
  • 61
  • 83
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)
Pranav C Balan
  • 113,687
  • 23
  • 165
  • 188