I searched for "hr001" and it returns all index having "hr001". This is ok. But I want to sort this array using searched text("hr001").
Look at given code. Here returns first and last element of this array when I searched "hr001" and array order is: "code": "SFHR001", "code": "HR001".
Since I searched for "hr001" and it fully matches to "code": "HR001", so i want to show "code": "HR001" as first element
var items = [
{
"id": 1,
"code": "SFHR001",
"property": "8''",
"description": "Half Round",
"productGroup": {
"id": 2,
"name": "Steel Files",
}
},
{
"id": 2,
"code": "MR004",
"property": "7''",
"description": "Polished",
"productGroup": {
"id": 3,
"name": "Pliers",
}
},
{
"id": 2,
"code": "HR001",
"property": "7''",
"description": "Fine Polished",
"productGroup": {
"id": 3,
"name": "Hand Reveter",
}
},
]
search = (item, searchedText) => {
return item["code"].toLowerCase().indexOf(searchedText.toLowerCase()) > -1
}
var ac = items.filter((item) => {
return search(item, 'hr001');
});
console.log(ac);
// var sortWithInputText = ac.sort(..........................)

ac.sort((a,b)=> a.code.toLowerCase().indexOf('hr001') - b.code.toLowerCase().indexOf('hr001'))something like this will do the work