How to sort an array in descending order in Ruby?
Last Updated :
10 Apr, 2024
In this article, we will discuss how to sort an array in descending order in ruby. We can sort an array in descending order through different methods ranging from using sort
the method with a block to using the reverse
method after sorting in ascending order
Sort an array in descending order using the sort method with a block.
The sort
method sorts the array in ascending order by default. By providing a custom block that compares elements in reverse order, the array can be sorted in descending order.
Syntax:
sort
: array.sort { |a, b| b <=> a }
Example: In this example, we use sort
method with a block that compares elements in reverse order (b <=> a
) to sort the array in descending order
Ruby
# Given array
array = [5, 2, 8, 1, 9]
# Sorting an array in descending order using the sort method with a block
sorted_array = array.sort { |a, b| b <=> a }
puts sorted_array # Output: Method 1 Output: [9, 8, 5, 2, 1]
Sort an array in descending order using sort_by method with a block.
The sort_by method sorts the array based on the values returned by the block. By negating the values returned by the block, the array can be sorted in descending order.
Syntax:
sort_by
: array.sort_by { |item| -item }
Example: In this example, we use sort_by
method with a block that returns the negation of each element to sort the array in descending order.
Ruby
# Given array
array = [5, 2, 8, 1, 9]
# Sorting an array in descending order using the sort_by method with a block
sorted_array = array.sort_by { |item| -item }
puts sorted_array # Output: Output: [9, 8, 5, 2, 1]
Sort an array in descending order using reverse method after sorting in ascending order.
In this method we first sort the array in ascending order using sort
or sort_by
, then reverses the order of elements,thus converting them in descending order. .
Syntax:
reverse
: array.sort.reverse
Example: In this example we use sort
to s
ort the array in ascending order and then reverses it using the reverse
method to achieve descending order.
Ruby
# Given array
array = [5, 2, 8, 1, 9]
# Sorting an array in descending order using the reverse method after sorting in ascending order
sorted_array = array.sort.reverse
puts sorted_array # Output: [9, 8, 5, 2, 1]
Similar Reads
How to use numpy.argsort in Descending order in Python The numpy.argsort() function is used to conduct an indirect sort along the provided axis using the kind keyword-specified algorithm. It returns an array of indices of the same shape as arr, which would be used to sort the array. It refers to value indices ordered in ascending order. In this article,
4 min read
How to Check an Array is Sorted or Not in PHP? Given an array, the task is to check whether the given array is sorted or not. Arrays are often used to store and manipulate data. One common task is to check if an array is sorted in ascending or descending order. This article will explore different approaches to determine whether an array is sorte
3 min read
How to Sort a Hash in Ruby? In this article, we will learn how to sort a Hash in ruby. We can sort the map by key, from low to high or high to low, using the sortBy method. Syntax:sorted_hash = original_hash.sort_by { |key, value| expression }.to_h Example 1: In this example, we sort a hash in ascending order Ruby # Define a h
2 min read
How to sort an Array in C# | Array.Sort() Method Set â 2 Array.Sort Method is used to sort elements in a one-dimensional array. There are 17 methods in the overload list of this method. Here we will discuss the following methods: Sort(Array, Int32, Int32, IComparer) Method Sort(Array, Array, Int32, Int32, IComparer) Method Sort(Array, Int32, Int32) Method
9 min read
How to Sort an Array alphabetically in Ruby? Sorting an array alphabetically is a common operation in Ruby and can be done using different methods. Sorting an array alphabetically refers to arranging its elements in the order of their lexemes either increased or decreased depending on such order. We will consider some methods that can help us
2 min read