Advertisement
nullzero

Ruby

Jan 20th, 2015
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 0.34 KB | None | 0 0
  1. def funny_sort(arr)
  2.     parse = lambda {|str| str.scan(/\d+/).fetch(0, "-1").to_i }
  3.     arr.sort {|x, y| parse.call(x) <=> parse.call(y) }
  4. end
  5.  
  6. puts funny_sort(["abc", "123", "abc321", "213abc", "1.2", "a.3.2", "-987"])
  7.  
  8. #=> ["abc", "1.2", "a.3.2", "123", "213abc", "abc321", "-987"]
  9. # key = -1,  1,     3,       123,   213,      321,      987
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement