24
4
We all know of different fancy sorting algorithms, but none of these give us numbers in a way that's easy to pronounce. To remedy this, I propose using PronunciationSort™, the most natural way to sort lists of numbers.
Pronunciation
The official rules for pronouncing numbers (in this challenge) is that the digits are pronounced one by one, and the resulting string is sorted in lexicographic order. As an example, this means that the number 845
is pronounced "eight four five"
, and should be sorted accordingly.
Negative numbers
Negative numbers are pronounced by prepending the word "minus"
. Thus, -23
is pronounced as "minus two three"
. Note that this causes negative numbers to end up in the middle of the output, right between numbers starting with 4
(four) and 9
(nine).
As a guide, the official order of words for PronunciationSort™ is:
- eight
- five
- four
- minus
- nine
- one
- seven
- six
- three
- two
- zero
That is,
8, 5, 4, -, 9, 1, 7, 6, 3, 2, 0
Input
A list of integers in the range \$[-999, 999]\$, containing at most 100 elements. Input as a list of strings is not permitted. If your language does not support input as list, it is permissible to give input as separate integers.
The input will not contain any invalid numbers, or any number starting with a 0 (except the number 0 itself). The input will generally not be sorted, it can be given in any order.
Output
The same integers, in PronunciationSort™ order. Note that the numbers should only be converted to their pronunciations to get the sorting, the output should not contain any strings.
Examples
For the examples, the middle step (wrapped in parentheses) only serves as a guide, and is not a part of the output.
[1, 2, 3] -> (['one', 'two', 'three']) -> [1, 3, 2]
[-1, 0, 1, 2] -> (['minus one', 'zero', 'one', 'two']) -> [-1, 1, 2, 0]
[-100, 45, 96] -> (['minus one zero zero', 'four five', 'nine six']) -> [45, -100, 96]
[11, 12, 13, 134, 135] -> (['one one', 'one two', 'one three', 'one three four', 'one three five']) -> [11, 13, 135, 134, 12]
There's also a script for verifying your results.
Related – Arnauld – 2018-09-27T10:02:48.230
1Despite the barrage of minor clarifications, nice challenge! – trichoplax – 2018-09-27T12:20:24.333
1@trichoplax I hope I have clarified the challenge a bit, I updated the Input section. – maxb – 2018-09-27T12:34:30.380
1Looks good. I've deleted all my comments that no longer apply – trichoplax – 2018-09-27T12:34:57.363
5Why doesn't "one" (pronounced "won") come after two and before zero? – Ben Miller - Remember Monica – 2018-09-27T18:50:58.197
3@BenMiller I can't recall if it was you who commented in the sandbox, but that comment gave me deja vu. To answer it here, I contemplated it, but I went with spelling to avoid spelling discussions (e.g. "two" vs "too", "won" or "wan") – maxb – 2018-09-27T19:57:42.867
17So it is actually more "spelling sort" than "pronounciation sort" :-) – Paŭlo Ebermann – 2018-09-27T20:20:32.680
3bummer. I was hoping they would be sorted by how difficult they are to pronounce... – NH. – 2018-09-28T15:08:46.727
2This challenge really should be renamed, since the sorting here has virtually nothing to do with pronunciation. It would be immensely more complex if it were pronunciation-based (e.g., four might come before five if you sort the monophthong ⟨ɔː⟩ before the diphthong ⟨aɪ⟩, but five before four if you sort a-derived ⟨a⟩ before o-derived ⟨ɔ⟩ – there is, to my knowledge, no established sorting order for pronunciation, neither in IPA nor in any other scheme). – Janus Bahs Jacquet – 2018-09-29T14:09:47.800
@JanusBahsJacquet you could make another (more difficult) challenge where all numbers had to be sorted by spelling, or define an order for pronunciation. I chose an easier ordering for my challenge as I like to keep my challenges easy enough for everyone to understand and appreciate, while still being fun to golf. I agree that the name isn't perfect, but I chose it because I thought it'd gather some more attention for the challenge. – maxb – 2018-09-29T15:05:26.617
Do you have to take locale setting into account? Or is this limited to English spelling? – Ole Tange – 2018-09-30T12:50:35.003
@OleTange this challenge is limited to English only for simplicity and giving everyone an equal chance. I'm not a linguist, but in theory there could be a language where pronunciation order would be equal to regular sorting order, which gives a 1-byte solution to anyone using that locale – maxb – 2018-09-30T12:54:26.587
@maxb Sorry, I meant: Should it work for any locale? It will still give an equal chance to everyone, but you would have to support all known locales, which would make this task considerably harder. – Ole Tange – 2018-09-30T13:00:26.267
@OleTange oh, I understand now! That would be considerably harder indeed, but it is not required for this challenge. If you want to make your own challenge, you're very welcome to use this one as inspiration. – maxb – 2018-09-30T13:30:20.923