0

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

9
  • Does this answer your question? Sort typeahead suggestions with the exact input at top Commented Feb 3, 2020 at 13:29
  • You want to match exact word or want to have partial matching as well ? Commented Feb 3, 2020 at 13:31
  • @HereticMonkey no. I want to sort an array using my searched text Commented Feb 3, 2020 at 13:32
  • @Md.RobiUllah, Do you want like this? codepen.io/manmur/pen/zYxgJLz Commented Feb 3, 2020 at 13:34
  • ac.sort((a,b)=> a.code.toLowerCase().indexOf('hr001') - b.code.toLowerCase().indexOf('hr001')) something like this will do the work Commented Feb 3, 2020 at 13:34

3 Answers 3

1

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

Sign up to request clarification or add additional context in comments.

Comments

1

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'));

Comments

1

Try this:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.