Query:
Can you guide me on sorting my data
array based on two criteria:
- Ensure that the
type
is prioritized at the top. - Arrange the counts from smallest to largest.
Here's what I've tried so far:
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 Output
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**//
// Sort by type first
data.sort((a,b) => a.type === 'first' ? -1 : 0)
// Then sort by count
data.sort((a,b) => a.count < b.count ? -1 ? (a.count > b.count ? 1 : 0)
I'm facing challenges in dealing with shared count values among different types. How can I achieve sorting based on both properties while maintaining the hierarchy of type and order of count?