Question:
How do I sort my data
array by two properties:
- where
type
is always on top and - where count is always from smallest to largest.
This is my effort:
var data = [
{type: 'first', count: '1'},
{type: 'second', count: '5'},
{type: 'first', count: '2'},
{type: 'second', count: '2'},
{type: 'second', count: '1'},
{type: 'first', count: '0'},
]
//Expected
var newData = [
{type: 'first', count: '0'},
{type: 'first', count: '1'},
{type: 'first', count: '2'},
{type: 'second', count: '1'},
{type: 'second', count: '2'},
{type: 'second', count: '5'},
]
//**Pseudo code**//
// Will put the types on top
data.sort((a,b) => a.type === 'first' ? -1:0)
// This will sort the count
data.sort((a,b) => a.count < b.count ? -1 ? (a.count > b.count ? 1:0)
Since count share the values between different types, I'm finding it difficult to solve it. How can I sort both these properties but keeping type always on top, and count always in order from small to large?