4

I have a bunch of objects in an array and would like to sort by a value that each object has. The attribute in question in each object is a numeric value.

For example:

[[1, ..bunch of other stuff],[5, ""],[12, ""],[3, ""],]

would become:

[[1, ..bunch of other stuff],[3, ""],[5, ""],[12, ""],]

I want to sort by the numerical value stored in each of the objects.

[5, 3, 4, 1, 2] becomes [1, 2, 3, 4, 5], however these numbers are stored inside objects.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303
Julio
  • 6,182
  • 11
  • 53
  • 65
  • 1
    Your question is ambiguous. Do you want to sort by the first element of each array, or by the first element and second, and so on? – sawa Apr 19 '11 at 01:01
  • 1
    The more effort you put into explaining your code and what it's doing, the better answers you'll receive. Provide an simple code example showing the object, or at least the attribute you want to use for sorting. – the Tin Man Apr 19 '11 at 05:23

4 Answers4

9

The other answers are good but not minimal. How about this?

lst.sort_by &:first
Peter
  • 127,331
  • 53
  • 180
  • 211
  • @Peter... Thanks for this, just one thing. What does the &:first mean/do... Been studying ruby for about 2 days, don't think i've come across it. – Julio Apr 19 '11 at 10:16
  • Yep, it's a bit weird :). See http://stackoverflow.com/questions/1961030/ruby-ruby-on-rails-ampersand-colon-shortcut. – Peter Apr 19 '11 at 18:35
3

The sort method can take a block to use when comparing elements:

lst = [[1, 'foo'], [4, 'bar'], [2, 'qux']]
=> [[1, "foo"], [4, "bar"], [2, "qux"]]
srtd = lst.sort {|x,y| x[0] <=> y[0] }
=> [[1, "foo"], [2, "qux"], [4, "fbar"]]
yan
  • 20,644
  • 3
  • 38
  • 48
  • thanks for this. Is there anyway to reference the attribute in the object when doing the sort. For example the attribute is called numeric_value. So could you perform the sort on this rather than a predefined location in the array? Thanks – Julio Apr 19 '11 at 10:21
  • @Julio: Yup, absolutely. Then the body of the block would be 'x.numeric_value <=> y.numeric_value' – yan Apr 19 '11 at 11:20
1

Assuming that you want to sort only according to the first element,

[[1, ..bunch of other stuff],[5, ""],[12, ""],[3, ""],].
sort_by{|n, *args| n}

or

[[1, ..bunch of other stuff],[5, ""],[12, ""],[3, ""],].
sort_by{|n, args| n}
sawa
  • 165,429
  • 45
  • 277
  • 381
1

When sorting objects and complex structures use sort_by. Sort_by performs a "Schwartzian Transform" which can make a major difference in sort speed.

Because you didn't provide enough information to be usable I'll recommend you read the docs linked above. You'll find its very easy to implement and can make a big difference.

the Tin Man
  • 158,662
  • 42
  • 215
  • 303