Sorting hashes in a Ruby array

I will write about the sorting of hashes contained in the array. Suppose you have a hash in an array like this:

array = [
  {
    :nickname  =>  "Mr. A",
    :image  =>  "default_user_image.jpeg ",
    :sum_size  =>  211.0,
    :max_size  =>  64.0
  },
  {
    :nickname  =>  "Mr. B",
    :image  =>  "/uploads/user/image/10/C2513F3.jpeg ",
    :sum_size  =>  215.0,
    :max_size  =>  50.0
  },
  {
    :nickname  =>  "Mr. C",
    :image  =>  "/uploads/user/image/6/FB6D582.jpeg ",
    :sum_size  =>  65.0,
    :max_size  =>  65.0
  }
]

When you want to sort in descending order of the value of : max_size.

sort_maxsize = array.sort_by! { |x| x[:max_size] }
sort_maxsize.reverse!

Execution result


  {
    :nickname  =>  "Mr. C",
    :image  =>  "/uploads/user/image/6/FB6D582.jpeg ",
    :sum_size  =>  65.0,
    :max_size  =>  65.0
  },
  {
    :nickname  =>  "Mr. A",
    :image  =>  "default_user_image.jpeg ",
    :sum_size  =>  211.0,
    :max_size  =>  64.0
  },
  {
    :nickname  =>  "Mr. B",
    :image  =>  "/uploads/user/image/10/C2513F3.jpeg ",
    :sum_size  =>  215.0,
    :max_size  =>  50.0
  }
]

I was able to sort well for : max_size!

Postscript

I told you in the comments, so I will add it! !!

In the case like this time, sign inversion seems to be smart even if you do not invert after arranging in ascending order!

sort_maxsize = array.sort_by! { |x| -x[:max_size] }

Thank you for your comment!

reference

Ruby Reference Array # sort

Ruby Reference Array # reverse

Recommended Posts

Sorting hashes in a Ruby array
Multiplication in a Ruby array
[Ruby] Extracting a two-dimensional array
Calculate the difference between numbers in a Ruby array
[Ruby] Array
How to change a string in an array to a number in Ruby
[Ruby] Extracting double hash in array
Implement a gRPC client in Ruby
Hanachan in Ruby (non-destructive array manipulation)
What is a Ruby 2D array?
[Ruby] I want to put an array in a variable. I want to convert to an array
I tried a calendar problem in Ruby
When seeking multiple in a Java array
Summary of hashes and symbols in Ruby
About Ruby Hashes
Ruby Learning # 14 Hashes
Class in Ruby
Sorting AtCoder ABC 111 C hashes to solve in Ruby, Perl and Java
About Ruby Hashes
Ruby two-dimensional array
Heavy in Ruby! ??
Ruby array manipulation
Escape processing when creating a URL in Ruby
[Understanding] Differences between hashes and arrays in Ruby
I made a Ruby extension library in C
(Ruby on Rails6) Creating data in a table
Create a native extension of Ruby in Rust
Do something like a JS immediate function in Ruby
Count the number of occurrences of a string in Ruby
About Ruby Hashes (continued)
About eval in Ruby
Examine the elements in the array using the [Ruby] includes? Method
[Ruby / Rails] Set a unique (unique) value in the class
What impressed me as a beginner in writing Ruby
How to launch another command in a Ruby program
Output triangle in Ruby
How to convert a file to a byte array in Java
[Programming complete] §5 Create a review management app in Ruby
I got stuck in a clone of a two-dimensional array
Variable type in ruby
Fast popcount in Ruby
Use Coveralls with GitHub Actions in a Ruby repository
Multidimensional array in Swift
[Ruby] Count an even number in an array using the even? Method
[Ruby] Searching for elements in an array using binary search
Difficulties in building a Ruby on Rails environment (Windows 10) (SQLite3)
How to divide a two-dimensional array into four with ruby
Determine that the value is a multiple of 〇 in Ruby
Convert a Java byte array to a string in hexadecimal notation
In Ruby you can define a method with any name
Read WAV data as a byte array in Android Java
How to retrieve the hash value in an array in Ruby
I searched for a web framework with Gem in Ruby
Learning Ruby with AtCoder 13 How to make a two-dimensional array
How to display a graph in Ruby on Rails (LazyHighChart)
How to add the same Indexes in a nested array
I want to create a Parquet file even in Ruby
I was confused because there was a split in the Array
Apply CSS to a specific View in Ruby on Rails
I wrote a C parser (like) using PEG in Ruby
[Ruby] How to batch convert strings in an array to numbers