I have the following data structure in Ruby (a hash where keys are strings, and values are arrays).
X = { "id": [2, 4, 1], "name": ["a", "b", "c"], "time": [1, 0, 2]}
I would like to sort the array associated with the field “time”, but I would like all other arrays to be sorted in a consistent manner. Example: after sorting, X should look like this.
X = {"id": [4, 2, 1], "name": ["b", "a", "c"], "time": [0, 1, 2]}
I solved this in a really ugly way (because I’m not sure how to do it). What I did was create a copy of time, then zip id and time, and sort it, then zip name and time_copy and sort it. Then unzip. I’m pretty sure this is an awful way to do it. Could someone else teach me a better method?
Using @tokland’s answer to another question and applying
values_atto the result: