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.

2
  • 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? Commented Apr 19, 2011 at 1: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. Commented Apr 19, 2011 at 5:23

4 Answers 4

9

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

lst.sort_by &:first
Sign up to request clarification or add additional context in comments.

2 Comments

@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.
Yep, it's a bit weird :). See stackoverflow.com/questions/1961030/….
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"]]

2 Comments

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: Yup, absolutely. Then the body of the block would be 'x.numeric_value <=> y.numeric_value'
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}

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.