I'm trying to sort an array of hashs based on multiple keys and multiple directions (ASC and DESC).
Assume the array looks like:
items = [ {field1: '1', field2: 5, field3: 5},
{field1: '1', field2: 1, field3: 3},
{field1: '3', field2: 3, field3: 2},
{field1: '3', field2: 1, field3: 8},
{field1: '7', field2: 5, field3: 6},
{field1: '7', field2: 5, field3: 1} ]
I want to create multiple_sort(items,options) method which works like:
multiple_sort(items, [{field: 'feed1', dir: 'asc'}, {field: 'feed3', dir: 'asc'}])
Will generate:
[ {field1: '1', field2: 1, field3: 3},
{field1: '1', field2: 5, field3: 5},
{field1: '3', field2: 3, field3: 2},
{field1: '3', field2: 1, field3: 8},
{field1: '7', field2: 5, field3: 1},
{field1: '7', field2: 5, field3: 6} ]
And
multiple_sort(items, [{field: 'feed1', dir: 'asc'}, {field: 'feed3', dir: 'desc'}])
Will output:
[ {field1: '1', field2: 5, field3: 5},
{field1: '1', field2: 1, field3: 3}
{field1: '3', field2: 1, field3: 8},
{field1: '3', field2: 3, field3: 2},
{field1: '7', field2: 5, field3: 6},
{field1: '7', field2: 5, field3: 1} ]
Any help would be appreciated.