Sort Strings and Numbers in Ruby

Sort Ruby String Array by the number in the string

The below would sort by the number in each string and not the string itself

array.sort_by { |x| x[/\d+/].to_i }
=> ["STRING: 1", "STRING: 2", "STRING: 3", "STRING: 4", "STRING: 5"]

descending order:

array.sort_by { |x| -(x[/\d+/].to_i) }
=> ["STRING: 5", "STRING: 4", "STRING: 3", "STRING: 2", "STRING: 1"]

Sort strings and numbers in Ruby

A general trick for solving tricky sorts is to use #sort_by, with the block returning an array having the primary and secondary sort order (and, if you need it, tertiary, etc.)

a = ['foo', 'bar', '1', '2', '10']  
b = a.sort_by do |s|
if s =~ /^\d+$/
[2, $&.to_i]
else
[1, s]
end
end
p b # => ["bar", "foo", "1", "2", "10"]

This works because of the way array comparison is defined by Ruby. The comparison is defined by the Array#<=> method:

Arrays are compared in an “element-wise” manner; the first element of ary is compared with the first one of other_ary using the <=> operator, then each of the second elements, etc… As soon as the result of any such comparison is non zero (i.e. the two corresponding elements are not equal), that result is returned for the whole array comparison.

Efficient way to sort an array of numbers and string in ruby?

One way is to partition your array by those elements being Integer, to make sure they remain first and then sort the elements of each array:

[4,2,'b',5,'c','a',7].partition { |e| e.is_a?(Integer) }.flat_map(&:sort)
# [2, 4, 5, 7, "a", "b", "c"]

How to sort an array of strings containing numbers and letters in ruby


array.sort_by { |item|
number, letter = *item.split
[letter, number.to_i]
}

Arrays compare as their first element; in case an element is equal, next element is compared.

Sort an Array of Strings by their Integer Values

I'll throw another method out there since it's the shortest way I can think of

a.sort_by(&:to_i)

Sort an Array Mixed With Integers and Strings - Ruby

This will give you the result:

i_want_dogs.sort_by {|x| x.to_s }

UPDATE:

Thanks @vacawama who points out that it will sort numbers alphabetically. If you need to sort number by it's value, other answers will be something you need to try.

Sort array of string with digits and characters in ruby

You're looking for a "natural" sort where the numeric substrings will be compared as numbers as the non-numeric parts will be compared like strings. Conveniently enough, arrays in Ruby compare element-by-element and your format is fairly regular so you can get away with a #sort_by call and a bit of mangling to convert "12mo-21-classic" to [12, 'mo-', 21, '-classic']. Something like this for example:

# This is a bit complicated so we'll give the logic a name.
natural_parts = ->(s) { s.match(/(\d+)(\D+)(\d+)(\D+)/).to_a.drop(1).map.with_index { |e, i| i.even?? e.to_i : e } }
array.sort_by(&natural_parts)

sort string containing numbers in ruby/rails


@locations = Location.all.sort { |a, b| b.facebook_likes.to_i <=> a.facebook_likes.to_i }

or

@locations = Location.all.sort_by { |a| -(a.facebook_likes.to_i) }

Sorting array of string by numbers


▶ a = ["10a","10b","9a","9b","8a","8b"]
▶ a.sort { |a,b| a.to_i == b.to_i ? a <=> b : a.to_i <=> b.to_i }
#=> [
# [0] "8a",
# [1] "8b",
# [2] "9a",
# [3] "9b",
# [4] "10a",
# [5] "10b"
#]

Hope it helps.



Related Topics



Leave a reply



Submit