Sorting an Array of Numbered String Values
·
176 words
·
1 minute read
This is an issue in programming when numbers are stored as strings - because as strings, when sorted alphabetically they will go by each digit, from first to last. You might encounter this problem when dealing with numbered file names, for example, which will be treated as strings but we might need to process them in order.
In our example below we use both the sort
and strconv
packages to sort the array and convert the strings to numbers within our comparison function. This conversion to integer gives us the reliable sorting.
|
|