let's say that I have an array of objects that is structured like below:
let fruits = [
{type: 'apple', count:1},
{type: 'apple', count:2},
{type: 'apple', count:3},
{type: 'orange', count:2},
{type: 'orange', count:3},
{type: 'orange', count:4},
{type: 'banana', count:3},
{type: 'banana', count:4}
]
I am trying to sort the array first by 'type', and then by 'count'. However, I want to do it in batches. That is, the sorted array would first show the smallest count entries per each type of fruit, whose orders are sorted by 'type', and then second smallest count entries, etc...The result should be like below:
let fruits = [
{type: 'apple', count:1},
{type: 'banana', count:3},
{type: 'orange', count:2},
{type: 'apple', count:2},
{type: 'banana', count:4},
{type: 'orange', count:3},
{type: 'apple', count:3},
{type: 'orange', count:4}
]
One way I can think about is to create new arrays from the original array to include the lowest counts, second lowest counts, etc... and then use the arrays to create a new array in the intended order, but looks like this will be a complicated code and wondered what would be the better way to do this.
Any suggestions/ideas? Thanks a lot in advance!
sort(), the comparison criterion is not consistent.