I have an array of table rows that need to be sorted by the field "data" ascending.
var data = [
{data: 5},
{data: 10, sticky: true},
{data: 90},
{data: 50, sticky: true},
{data: 20, sticky: true},
{data: 70},
];
But there is a catch. I need rows with the "sticky" flag to be sorted to the top of the table. My sort function currently looks like this:
data.sort(function(a, b) {
if (a.sticky) return -1;
if (b.sticky) return +1;
return mySortFunction(a, b);
});
which yields
[
{data: 10, sticky: true},
{data: 50, sticky: true},
{data: 20, sticky: true},
{data: 5},
{data: 70},
{data: 90}
]
The problem is, I need the "sticky" rows to be sorted by "data" (asc) as well. Is there an easy way to do that without splitting the data into a sticky and non-sticky group first and then running the sortfunction against both groups?