Sorting

From Rosetta Code
Revision as of 20:45, 22 January 2007 by rosettacode>Gfannes
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Task
Sorting
You are encouraged to solve this task according to the task description, using any language you may know.

Sort an Array of Strings, from large to small and lexicographic for Strings of equal length

Ruby

 #create a test array
 ary=['Long','words','are','more','interesting','than','short','ones']
 ary.sort do |wx,wy|
   if wx.length==wy.length
     wx <=> wy
   else
     wy.length <=> wx.length
   end
 end
 # => ["interesting", "short", "words", "Long", "more", "ones", "than", "are"]