ruby on rails - Is it possible conditional sort in hash -
Is conditional conditional in hashes
my hash such as {1 => "10", 2 => "20", 3 => "30", 4 => "40", 5 => "50", 6 => "60", 7 => "70", 8 => "80", 9 = > "80", 9 => "90", 1 => "10", 2 = "0">
Add condition to this code
Previous> operation_hour_hash.sort {| Key, value | Key [1] & lt; = & Gt; Value [1]}
Of course, you can do something like this:
& gt; & Gt; {1 = & gt; "10", 2 = & gt; "20", 3 = & gt; "30", 4 = & gt; "40", 5 = & gt; "50", 6 = & gt; "60", 7 = & gt; "70", 8 = & gt; "80", 9 = & gt; "90"} {sort | P, N. (P [1] .to_i> = 70 & amp; amp; amp; amp; amp; amp; [1] .to_i <70)? -1: (P [1] .to_i <70 & amp; amp; amp; amp; amp; amp; [1] .to_i> = 70)? 1: p [1] .to_i & lt; = & Gt; N [1] .to_i} = & gt; [2, "20"], [3, "30"], [[7, "70"], [8, "80"], [9, "90"], [1, "10"], [ 4, "40"], [5, "50"], [6, "60"]]
But, sort a hash really does not quite understand much. Before it is actually sorted, it is converted into an array of [key, value] pairs, then sorted by -1,0,1
If you need to use something like class.
Comments
Post a Comment