"Wilka" <jvyxn / evghnyvfgvp.pbz> writes: > Does Ruby already have an compare method that takes into account numbers > inside a string? i.e. > > strings = Array.new > strings.push "Name 300" > strings.push "Name 1" > strings.push "Name 10" > strings.push "Name 2" > > strings.sort do |a, b| > # something to put here to sort like a human would > end Here are a couple of ways. The second may or may not be more efficient, as it only does the pattern match and to_i once for each string in the array. strings = [ "Name 300", "Name 1", "Name 10", "Name 2" ] # an inefficient way (if there are large numbers of strings) digits = /\d+$/ res = strings.sort { |a, b| digits.match(a)[0].to_i <=> digits.match(b)[0].to_i} # a more efficient way (perhaps) res = strings. collect {|e| [ digits.match(e)[0].to_i, e ] }. sort {|a, b| a[0] <=> b[0] }. collect {|e| e[1]} Regards Dave