Your task is to sort an array containing the strings "quarter", "dime", "nickel", and "penny" any number of times in no specific order and sort them so that they are in this order: quarter dime nickel penny (in other words, greatest to least monetary value).


  1. Your program must take an array as input containing the names of U.S coins and sort them from greatest to least by monetary value.
    • For those who are not from the U.S or don't use change, the values of U.S coins, from greatest to least, are:
      • Quarter: 25 cents
      • Dime: 10 cents
      • Nickel: 5 cents
      • Penny: 1 cent
  2. You may sort this array in any way you wish, as long as the output is ordered by the monetary values shown above.
  3. Input can be taken in any way, be it command-line arguments or STDIN.
  4. An input array would be all lowercase strings, something like this:
    • quarter dime nickel nickel quarter dime penny penny
  5. The actual format of input and output is up to you.

Test Cases

"penny nickel dime quarter" 
-> "quarter dime nickel penny"

"nickel penny penny quarter quarter quarter dime dime dime dime"
-> "quarter quarter quarter dime dime dime dime nickel penny penny"

"quarter dime nickel nickel quarter dime penny penny"
-> "quarter quarter dime dime nickel nickel penny penny"

This is , so standard rules & loopholes apply.

  • 1
    \$\begingroup\$ Let us continue this discussion in chat. \$\endgroup\$
    – ckjbgames
    Feb 3, 2017 at 19:24
  • 1
    \$\begingroup\$ All test cases should include output. In the mobile the second and third cases are shown in two lines, so it looks as if the second line is the output \$\endgroup\$
    – Luis Mendo
    Feb 3, 2017 at 19:33
  • 4
    \$\begingroup\$ i'm canadian, can i assume the input has no pennies? ;) \$\endgroup\$ Feb 3, 2017 at 21:48
  • 1
    \$\begingroup\$ @undergroundmonorail Sadly, no. \$\endgroup\$
    – ckjbgames
    Feb 5, 2017 at 13:46
  • 1
    \$\begingroup\$ What happened to loonies and half-dollars? \$\endgroup\$
    – Adám
    Feb 6, 2017 at 6:29

34 Answers 34


Ruby, 27 bytes


Smalltalk, 42 bytes

a sort:[:s :t|s allButFirst>t allButFirst]


a              input array
sort:          sort message, criterion in the block
[:s :t |       block arguments, represent entries of the input array
allButFirst    copy the receiving string without its first character
>              alphabetic comparison

Perl 6 (19 bytes)

Takes a list of strings.


In action:

my &f = *.sort(*.comb[5%*]);
say f(<quarter dime nickel nickel quarter dime penny penny>);

Thunno 2, 3 bytes


Try it online!

Port of ETHProductions's Japt answer - sort by 9th character.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.