I have this months array:
["January", "March", "December" , "October" ]
And I want to have it sorted like this:
["January", "March", "October", "December" ]
I'm currently thinking in a "if/else" horrible cascade but I wonder if there is some other way to do this.
The bad part is that I need to do this only with "string" ( that is, without using Date object or anything like that )
What would be a good approach?
If I had a way to supply a custom sorting order, I'd create a list defining the correct order:
correct = List("January", "February", "March", ...)
And then sort by the position in that list, something like:
toSort.sort(a, b) => compare(correct.index(a), correct.index(b))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With