I am new to Ruby, could someone help?
I have some product data Json that I need to sort by the expiry date, however everything thing I have tried with .sort_by so far is erroring.
The Json is in this format
{"wh_Repeating":[
{
"wh": {
"Item_Number": "111166",
"Expiry_Date": "2023-05-05"
}
},
{
"wh": {
"Item_Number": "111167",
"Expiry_Date": "2023-05-01"
}
},
{
"wh": {
"Item_Number": "111168",
"Expiry_Date": "2023-05-09"
}
}]}
in Ruby that shows as
{:wh_Repeating=>[
{:wh=>{:Item_Number=>"111166", :Expiry_Date=>"2023-05-05"}},
{:wh=>{:Item_Number=>"111167", :Expiry_Date=>"2023-05-01"}},
{:wh=>{:Item_Number=>"111168", :Expiry_Date=>"2023-05-09"}}
]}
tried alsorts
latest attempt was
sorted = jsonIn["wh_Repeating"]
sorted.sort_by { |k,v| v[:"Expiry_Date"] }
puts sorted
which gave me
undefined method `sort_by’ for nil:NilClass (NoMethodError)
(Exception)
3
Answers
I would do it like this:
Your hash keys are symbols not strings.
jsonIn["wh_Repeating"]
should bejsonIn[:wh_Repeating]
Also,
sorted.sort_by { |k,v| v[:"Expiry_Date"] }
does not mutatesorted
.sort_by
does not mutate the receiver. In other words, the value ofsorted
remains the same. There is a bang version (sort_by!
) that does mutate (a side-effect) but the use of mutating functions is discouraged.This does what you want to do.
I have assumed the original hash (
h
below) is not to be mutated (modified).As this hash has a single key (
:wh_Repeating
), we can simply write the structure of the desired hash asand then compute the value of
:wh_Repeating
.The original hash
h
is unchanged, which can be easily verified.