I need to write a multi_sort method to sort an array of hash which
accepts a hash as an argument, eg: { a_sort: 1, display_sort: 1 }. This
hash is sort fields and sort direction. (1 means ascending, -1 means
descending).
items = [ {name: ‘Album 1’, a_sort: 5, display_sort: 3},
{name: ‘Album 2’, a_sort: 1, display_sort: 5},
{name: ‘Album 3’, a_sort: 3, display_sort: 2},
{name: ‘Album 5’, a_sort: 1, display_sort: 8},
{name: ‘Album 7’, a_sort: 5, display_sort: 1},
{name: ‘Album 7’, a_sort: 5, display_sort: 6} ]
multi_sort(items, {a_sort: 1, display_sort: 1})
I can’t figure it out after 3 hours. The expected output is the array
that is sorted correctly.
[ {name: 'Album 2', a_sort: 1, display_sort: 5},
{name: 'Album 5', a_sort: 1, display_sort: 8},
{name: 'Album 3', a_sort: 3, display_sort: 2},
{name: 'Album 7', a_sort: 5, display_sort: 1}
{name: 'Album 1', a_sort: 5, display_sort: 3},
{name: 'Album 7', a_sort: 5, display_sort: 6} ]