I have an array 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.
my_array = [4, 2, 5, 3, 1]
hash = [
{"id" => 1, "field_name" => "foo"},
{"id" => 2, "field_name" => "bar"},
{"id" => 3, "field_name" => "abc"},
{"id" => 4, "field_name" => "zsh"},
{"id" => 5, "field_name" => "kql"},
{"id" => 6, "field_name" => "plo"},
{"id" => 7, "field_name" => "cde"}
]
Needed output
[
{"id" => 4, "field_name" => "zsh"},
{"id" => 2, "field_name" => "bar"},
{"id" => 5, "field_name" => "kql"},
{"id" => 3, "field_name" => "abc"},
{"id" => 1, "field_name" => "foo"},
{"id" => 6, "field_name" => "plo"},
{"id" => 7, "field_name" => "cde"}
]
Appreciate any help and thanks in advance!
"id"=>6and"id"=>7were at positions 1 and 2? Those places will be taken by"id"=>4and"id"=>2during the sort, so they have to move somewhere else.arraybasically so that they come at top (based on their position in the array) and the rest come just after those sorted one