How to sort Ruby Hash based on date?

Posted by Eki Eqbal on Stack Overflow See other posts from Stack Overflow or by Eki Eqbal
Published on 2014-08-22T03:59:37Z Indexed on 2014/08/22 4:20 UTC
Read the original article Hit count: 184

Filed under:
|
|

I have a hash object with the following structure:

{"action1"=>
  {"2014-08-20"=>0,
   "2014-07-26"=>1,
   "2014-07-31"=>1
  },
 "action2"=>
  {"2014-08-01"=>2,
   "2014-08-20"=>2,
   "2014-07-25"=>2,
   "2014-08-06"=>1,
   "2014-08-21"=>1
 }
 "action3"=>
  {"2014-07-30"=>2,
   "2014-07-31"=>1,
   "2014-07-22"=>1,
  }
}

I want to sort the hash based on the date and return back a Hash(Not array). The final result should be:

{"action1"=>
  {"2014-07-26"=>1,
   "2014-07-31"=>1,
   "2014-08-20"=>0
  },
 "action2"=>
  {"2014-07-25"=>2,
   "2014-08-01"=>2,
   "2014-08-06"=>2,
   "2014-08-20"=>1,
   "2014-08-21"=>1
 }
 "action3"=>
  {"2014-07-22"=>1,
   "2014-07-30"=>2,
   "2014-07-31"=>1
  }
}

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about ruby