I am trying to sort an array of strings. However some are numbers, some numbers with letters appended, some are letters with numbers appended, and some are purely text. For example:
array = ["100", "2A", "25", "Blue", "1", "X2", "X1", "Yellow", "2"]
I would like the sort the list so it displays the numbers and number-strings in together (1, 2, 2A), then the string-numbers (X1, X2), then finally the strings at the end (Blue, Yellow).
I have tried using array.sort() but this has not worked. What I would like to get returned would be:
["1","2","2A","25","100","X1","X2","Blue","Yellow"]
But instead this would sort lexicographically:
["1","100","2","25,"2A","Blue","X1","X2","Yellow"]
I have also tried splitting into integers and strings, sorting then combining after. But this does not take into account the numbers-string and string-number elements.
Is there any way to make this work?
.sort((a,b) => a.localeCompare(b))"2"and"Yellow"it's got to figure out that the first one is a number and the second one isn't. For"X2"it's got to find the digits embedded in the string. If you have"AA22B"and"b10a", well, you have to figure that out too.