lundi 30 juillet 2018

Sorting array of hashes based on multiple key/value pairs in Ruby

I have an array of hashes which I have to sort based on four different key value pairs.

Here is the array of hashes, I want First I want to sort based on "to" then based on "from"

[ 
  {
    :title=>"Working as a SSE",
    :organisation=>{:id=>428, :name=>"google"},
    :from=>"2018-6-1",
    :to=>nil
  }, {
    :title=>"Concatenate two video files to single video in players",
    :organisation=>{:id=>197, :name=>"UNFPA"},
    :from=>"2014-1-1",
    :to=>"2015-12-1"
  }, {
    :title=>"Highcharts Demo",
    :organisation=>{:id=>6, :name=>"UNDFS"},
    :from=>"2016-1-1",
    :to=>"2017-6-1"
  }, {
    :title=>"Working as a Judicial Affairs",
    :organisation=>{:id=>427, :name=>"swtp"},
    :from=>"2017-1-1",
    :to=>"2018-6-1"
  }
]

I want the sorting should happen in the following order

  1. First sort is based on "to"
  2. Second sort is based on "from"
  3. Third sort is based on "organisation name"
  4. Finally, sort based on "title"

Can anyone help me sort the array of hashes?

Aucun commentaire:

Enregistrer un commentaire