2
test = [["movie5name","2014"],["movie2name","2016"],["movie3name","2016"],["movie3name","2017"]]
testNew = test.sort_by{|n| [-n[1],n[0]]}

I was trying to sort above array by first sorting based on descending order of year then based on the acesding order of the movie name. However, it shows undefined method error because the negative sign before n:(

0

1 Answer 1

4

That's because you're trying to make a string negative, try following code:

testNew = test.sort_by{|n| [-n[1].to_i,n[0]]}

Assuming that n[1] is always convertible to a number.

Sign up to request clarification or add additional context in comments.

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.