Mega Code Archive

 
Categories / Ruby / Array
 

Get the top 5 elements

l = [1, 60, 21, 100, -5, 20, 60, 22, 85, 91, 4, 66] sorted = l.sort # The top 5 p sorted[-5...sorted.size] # => [60, 66, 85, 91, 100]