I have an array of objects that I want to sort based on whether or not they include a string.
I then want to sort those that do match the string by chronological order, and then the rest by chronological order.
So that
const role = waiter
const jobs = [
{ 
"date": "2021-09-30T00:00:00",
"role": [chef, porter]
},
{ 
"date": "2021-09-28T00:00:00",
"role": [waiter, chef]
},
{ 
"date": "2021-09-29T00:00:00",
"role": [waiter, chef]
},
{ 
"date": "2021-09-01T00:00:00",
"role": [chef]
},
]
Should become:
[
{ 
"date": "2021-09-28T00:00:00",
"role": [waiter, chef]
},
{ 
"date": "2021-09-29T00:00:00",
"role": [waiter, chef]
},
{ 
"date": "2021-09-01T00:00:00",
"role": [chef]
},
{ 
"date": "2021-09-30T00:00:00",
"role": [chef, porter]
},
]
So far the only solution I can find is to separate the array into two separate arrays. Sort them individually and then merge them back into one.
My current solution:
 const arrOne = jobs
    .filter((j) => j.role.includes(role))
    .sort((a, b) =>
      compareDesc(
        a.date,b.date
      )
    );
 const arrTwo = jobs
    .filter((j) => !j.role.includes(role))
    .sort((a, b) =>
      compareDesc(
        a.date,b.date
      )
    );
const finalArr = [...arrOne,...arrTwo]
Is there a more elegant solution?
Preferably one that doesn't require splitting the arrays into separate arrays?
 
     
    