How to Sort a Hash in Ruby? Last Updated : 26 Mar, 2024 Comments Improve Suggest changes Like Article Like Report 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 hash hash = {"Zash" => 30, "Jhavesh" => 20, "Charlie" => 50} # Sort the hash by value in ascending order sorted_hash = hash.sort_by { |_, value| value }.to_h puts sorted_hash # Output: {"Jhavesh"=>20, "Zash"=>30, "Charlie"=>50} Output{"Jhavesh"=>20, "Zash"=>30, "Charlie"=>50} Example 2: In this example we sort a Hash by Values in Descending Order Ruby # Define a hash hash = {"Zash" => 30, "Jhavesh" => 20, "Charlie" => 50} # Sort the hash by value in descending order sorted_hash_desc = hash.sort_by { |_, value| -value }.to_h puts sorted_hash_desc # Output: {"Charlie"=>50, "Zash"=>30, "Jhavesh"=>20} Output{"Charlie"=>50, "Zash"=>30, "Jhavesh"=>20} Example 3: In this example we sort a Hash by Keys in Ascending Order Ruby # Define a hash hash = {"Zash" => 30, "Jhavesh" => 20, "Charlie" => 50} # Sort the hash by key in ascending order sorted_hash_keys = hash.sort_by { |key, _| key }.to_h puts sorted_hash_keys # Output: {"Charlie"=>50, "Jhavesh"=>20, "Zash"=>30} Output{"Charlie"=>50, "Jhavesh"=>20, "Zash"=>30} Comment More infoAdvertise with us Next Article How to Parse Hash in Ruby? M mishraaabcf9 Follow Improve Article Tags : Ruby Similar Reads How to convert String to Hash in Ruby? In this article, we will discuss how to convert string to hash in ruby. We can convert a string to a hash through different methods ranging from parsing JSON and parsing YAML to custom string parsing. Table of Content Converting string to hash using JSON parsingConverting string to hash using YAML p 2 min read How to Parse Hash in Ruby? Parsing a hash in Ruby entails gaining access to its keys and values, iterating over them, and carrying out any necessary actions. The article focuses on discussing the ways to parse a hash in Ruby. Table of Content Iterating through a HashAccessing Hash ElementsSorting a HashIterating through a Has 2 min read How to convert Array to Hash in Ruby? In this article, we will discuss how to convert an array to a hash in Ruby. Converting an array to a hash can be useful when we have data in an array format and need to organize it into key-value pairs for easier access and manipulation. Table of Content Converting array to hash using Hash::[] const 3 min read Ruby | Hash to_a method Hash#to_a() is a Hash class method which gives a nested array of the key-value pair. Syntax: Hash.to_a() Parameter: Hash values Return: array representation of the hash Example #1 : Ruby # Ruby code for Hash.to_a() method # declaring Hash value a = {a:100, b:200} # declaring Hash value b = {a:100, c 2 min read How to Convert Hash to JSON in Ruby? Ruby Hash is an unordered set of data in key-value pairs. These are mutable and can store multiple data types. JSON stands for Javascript Object Notation and is a human-readable file format that is commonly used in web services and API calls. In this article, we will discuss how to convert a hash to 2 min read Ruby | Hash to_s method Hash#to_s() is a Hash class method which gives the string representation of the hash. Syntax: Hash.to_s() Parameter: Hash values Return: string representation of the hash. Example #1 : Ruby # Ruby code for Hash.to_s() method # declaring Hash value a = {a:100, b:200} # declaring Hash value b = {a:100 1 min read Like