I am trying to sort large inputs in the fastest way in ascending order. The code is something like this:
t=gets
ti=t.to_i
r=[]
for i in(0..ti)
k=gets
r[i]=k.to_i
end
r.sort_by{|x| -x.last}
This is giving me an error saying undefined method 'last' for nil:nilclass <nomethoderror>
from tsort.rb: in sort_by
from tsort.rb in 'each'
from tsort.rb in 'sort_by'
I don’t know where am I wrong.
That’s what I have tried for sorting an array…which is r[] which has all the numbers in t! Can anyone please help.
My inputs are less than 10^6!
I can’t reproduce your exact error, I get
undefined method 'last' for n:Fixnum (NoMethodError). That makes sense, because you’re calling thelastmethod onx, which will hold the values of yourArrayr, allFixnums, which do not have alastmethod.It should work if you replace the last line with:
The
sortmethod will sort yourArrayin ascending order by default.