Q:

Sorting a Ruby array

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!

A:

I can't reproduce your exact error, I get undefined method 'last' for n:Fixnum (NoMethodError). That makes sense, because you're calling the last method on x, which will hold the values of your Array r, all Fixnums, which do not have a last method.
It should work if you replace the last line with:
r.sort

The sort method will sort your Array in ascending order by default.

A:

If you want to sort a list of integers taken from STDIN I suggest something like the following:
lines = STDIN.readlines.map { |x| x.strip.to_i }.sort
puts lines.join(', ')

It's cleaner, more rubyish and faster (read the documentation for Enumerable.sort_by to see why sort is a better alternative to sort_by).
I also see your code expects a number that says how many lines to read. You can get the same behavior by modifying the example above as follows:
line_count = gets.strip.to_i
lines = (1..line_count).collect { gets.strip.to_i }.sort
puts lines.join(', ')

