Sort_by


#1

I am not abe to understand sort_by funcionality i.e. what is the syntax and how do we use it ?
I am also not able to uunderstand how reverse will work.


#2

.sort_by

Syntax:

[1, 4, 3, 7, 2, 9].sort_by {|i| -i}             # => [9, 7, 4, 3, 2, 1]

Usage:

["bc", "def", "a"].sort_by {|word| word.length} # => ["a", "bc", "def"]

.reverse

Syntax:

[1, 2, 3].reverse # => [3, 2, 1]

Usage: straightforward.


#3

Thank You for your explanation.It just cleared my understanding.