Ruby | Enumerable sort() function Last Updated : 05 Dec, 2019 Comments Improve Suggest changes Like Article Like Report 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: ruby # Ruby program for sort method in Enumerable # Initialize enu = (1..10) # Prints enu.sort Output: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] Example 2: ruby # Ruby program for sort method in Enumerable # Initialize enu = [10, 9, 8, 12, 10, 13] # Prints enu.sort {|a, b| a <=> b} Output: [8, 9, 10, 10, 12, 13] Comment More infoAdvertise with us Next Article Ruby | Enumerable sort() function G gopaldave Follow Improve Article Tags : Ruby Ruby-Methods Ruby Collections Ruby Enumerable-class Similar Reads Ruby | Enumerable sort_by() function 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. S 1 min read Ruby | Enumerable sum() function The sum() of enumerable is an inbuilt method in Ruby returns the sum of all the elements in the enumerable. If a block is given, the block is applied to the enumerable, then the sum is computed. If the enumerable is empty, it returns init. Syntax: enu.sum { |obj| block } Parameters: The function acc 1 min read Ruby | Enumerable take() function The take() of enumerable is an inbuilt method in Ruby returns the first N elements from the enumerable. If it does not have N elements, then it returns the entire enum. Syntax: enu.take(n) Parameters: The function accepts N which specifies the number of elements to be returned. Return Value: It retu 1 min read Ruby | Enumerable select() function The select() of enumerable is an inbuilt method in Ruby returns the items in the enumerable which satisfies the given condition in the block. It returns an enumerator if no block is given. Syntax: enu.select { |obj| block } Parameters: The function takes a block whose condition is used to find the e 1 min read Ruby | Enumerable uniq() function The uniq() of enumerable is an inbuilt method in Ruby returns an array removing all the duplicates in the given enum. Syntax: enu.uniq Parameters: The function accepts no parameter. Return Value: It returns an array. Example 1: Ruby # Ruby program for uniq method in Enumerable # Initialize enu = [1, 1 min read Like