VOOZH about

URL: https://www.geeksforgeeks.org/ruby/ruby-enumerable-sort_by-function/

⇱ Ruby | Enumerable sort_by() function - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Ruby | Enumerable sort_by() function

Last Updated : 12 Jul, 2025
The sort_by() of enumerable is an inbuilt method in Ruby sorts enum using a set of keys generated by mapping the values in enum through the given block. The returned result is not guaranteed to be stable, it is unstable when the comparison is equal. It returns an enumerator when no block is given.
Syntax: enu.sort_by { |obj| block } Parameters: The function accepts a block. Return Value: It returns the an array.
Example 1: Output:
[10, 22, 14, 19]
Example 2: Output:
Enumerator: [10, 14, 22, 19]:sort_by
Comment