Md. Robi Ullah
Md. Robi Ullah

Reputation: 2152

How to sort an array using a searched text in javascript?

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(..........................)

enter image description here

Upvotes: 0

Views: 715

Answers (3)

Max
Max

Reputation: 1048

Try this:

items
.filter(el => el.code.toLowerCase().indexOf('hr001')>-1)
.sort((a,b)=> a.code.toLowerCase() < b.code.toLowerCase() ? -1 : 1)

Upvotes: 1

Aezur
Aezur

Reputation: 335

You seem to be looking for a 'sort by field' function.

JS's Array.prototype.sort function takes an optional parameter that is it's 'sort algorithm'. This is just a function that takes 2 params and returns -1 if the first param should be indexed first, 1 if the second should be indexed first, and 0 if they are equal.

https://www.w3schools.com/js/js_array_sort.asp

function sortObjectByField(a, b, field) {
  if (a[field] < b[field]) {
    return -1;
  }
  if (a[field] > b[field]) {
    return 1;
  }
  return 0;
};

items.sort((a, b) => sortObjectByField(a, b, 'code'));

Upvotes: 1

Keith
Keith

Reputation: 24181

You need some sort of logic to decide what is the closest,..

One simple example might be to base on length, so were the result length and length of search term are near, these go to the top. You could do more fancier scoring than this, but you will need to decide what logic this pertains. eg. There is even something called a soundex algorithm, you might want to merge with this etc. https://en.wikipedia.org/wiki/Soundex

But using the simple length test as a guide, below is a simple example. It also falls back to simple string comparing if the score's of each item are equal.

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
    
}

const searchTerm = 'hr001';

function score(a) {
  return Math.abs(searchTerm.length - a.code.length);
}


var ac = items.filter((item) => {
  return search(item, searchTerm);
}).sort((a, b) => {
  //sort by score, if equal fall back to compare.
  return score(a) - score(b) ||
    a.code.localeCompare(b.code);
});

console.log(ac);

// var sortWithInputText = ac.sort(..........................)

Upvotes: 1

Related Questions