I am wanting to sort an array using regular expression such that after the array is sorted, all the dishes are included in results including both matched and unmatched ones. Currently only few matched ones are in the sorted array. If I have two words to be search in regular expression then it should search all the two words independently and finally get the matched items as well as unmatched items. If i search for 'Fish CUrry' then it should look for both words independently and get the results and also add all the unmatched results at the end of sorted array. Here unmatched is 'Biryani' and all other are matched.
let allDishes = [
{
"DishId": 66,
"DishName": "Fish CUrry",
"DateCreated": "2021-10-21T11:19:28.000Z",
},
{
"DishId": 65,
"DishName": "Fish CUrry Masala",
"DateCreated": "2021-10-21T11:19:28.000Z",
},
{
"DishId": 64,
"DishName": "Chilli Fish CUrry Masala",
"DateCreated": "2021-10-21T11:19:28.000Z",
},
{
"DishId": 63,
"DishName": "Mutton CUrry",
"DateCreated": "2021-10-21T11:19:28.000Z",
},
{
"DishId": 62,
"DishName": "Biryani",
"DateCreated": "2021-10-21T11:19:28.000Z",
}
]
Below is the code that I have written:
let value = 'Fish CUrry';
let regex = new RegExp(`^${value}`, `i`);
const sortedArr = allDishes
.filter(x=>regex.test(x.DishName))
.sort((a, b) =>a.DishName.localeCompare(b.DishName));
Current result from above code is below:
[
{
"DishId": 66,
"DishName": "Fish CUrry",
"DateCreated": "2021-10-21T11:19:28.000Z",
},
{
"DishId": 65,
"DishName": "Fish CUrry Masala",
"DateCreated": "2021-10-21T11:19:28.000Z",
}
]
Expected sorted result needed should look like below:
[
{
"DishId": 66,
"DishName": "Fish CUrry",
"DateCreated": "2021-10-21T11:19:28.000Z",
},
{
"DishId": 65,
"DishName": "Fish CUrry Masala",
"DateCreated": "2021-10-21T11:19:28.000Z",
},
{
"DishId": 64,
"DishName": "Chilli Fish CUrry Masala",
"DateCreated": "2021-10-21T11:19:28.000Z",
},
{
"DishId": 63,
"DishName": "Mutton CUrry",
"DateCreated": "2021-10-21T11:19:28.000Z",
},
{
"DishId": 62,
"DishName": "Biryani",
"DateCreated": "2021-10-21T11:19:28.000Z",
}
]
CodePudding user response:
There is a simpler version of allDishes
but it doesn't change the algorithm. First of all you want to find all dishes which contains any of word in sentence so you need in regexp replace all space
s with |
symbol. This symbol means or
in regexp. Then I just firstly sort the given array and find all matched dishes. At the end from sorted array add all dishes which isn't in resultArr
const allDishes = [
{
"DishName": "Fish CUrry",
},
{
"DishName": "Fish CUrry Masala",
},
{
"DishName": "Mutton CUrry",
},
{
"DishName": "Chilli Fish CUrry Masala",
},
{
"DishName": "Biryani",
}
];
const value = 'Fish CUrry'.replace(' ', '|');
const regex = new RegExp(`^${value}`, `i`);
const sortedArr = allDishes.sort((a, b) => a.DishName.localeCompare(b.DishName));
const resultArr = allDishes.filter(x => regex.test(x.DishName));
for (const dish of sortedArr) {
if (!resultArr.includes(dish)) resultArr.push(dish);
}
console.log(resultArr);