Question:
I have anarray
and a hash
and what I want to achieve is to sort the hash
(with each id
inside each hash) based on the sort that is in the array and if the id
(in hash
) doesn’t exist in my_array
, they should not be deleted and just be pushed down in the sort.Answer:
my_array
from that given in the question and have renamed the array to something more appropriate.<=>
.Below I’ve listed the arrays used by
sort_by
for each element of arr
.[type, x]
is the array sort_by
uses for sorting, it will put all elements for which type 0
before those for which type = 1
. To break ties it orders elements by x
. For type = 1
, x
is the index of the element, which keeps them in order at the end.If you have better answer, please add a comment about this, thank you!