VOOZH about

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

⇱ Ruby | Enumerable sort() function - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Ruby | Enumerable sort() function

Last Updated : 12 Jul, 2025
The sort() of enumerable is an inbuilt method in Ruby returns an array which contains the enum items in a sorted order. The comparisons are done using operator or the optional block. The block must implement a comparison between a and b and return an integer less than 0 when b follows a, 0 when a and b are equivalent, or an integer greater than 0 when a follows b. The result returned is not stable. The order of the element is not stable when the comparison of two elements returns 0.
Syntax: enu.sort { |a, b| block } Parameters: The function accepts an optional comparison block. Return Value: It returns the an array.
Example 1: Output:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Example 2: Output:
[8, 9, 10, 10, 12, 13]
Comment