2

I've been staring at sort forums forever now and I officially accept that i do not understand whats going on.

I have an array called "people" with multiple index's each array index looks like this

("Steven", "32", "Detroit", "single")

("Fred", "19", "Boston", "married")

("Jason", "25", "san_fransisco" "single")

I want to sort these index's by city (3rd entry of an index)

I've looked at so many different websites and i'm not following this at all. Can someone help me out with an answer and briefly explain whats going on? Is this even possible using an array?

Thanks!

1
  • Do you mean you have an array of arrays (which is trivial to sort), or something else? Commented Oct 16, 2012 at 19:09

1 Answer 1

10
people = [
  ["Steven", "32", "Detroit", "single"],
  ["Fred", "19", "Boston", "married"],
  ["Jason", "25", "san_fransisco", "single"],
]

people.sort_by{|p| p[2]}
Sign up to request clarification or add additional context in comments.

1 Comment

Ugh. I can't believe it was that easy. Thank you!!

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.