0

I have a JSON object containing a number of strings. I also have a match string . Now I wish to arrange the object of strings on the basis of how closely (more) they match with match string.

How this can be done using Javascript.

Suppose, I searched for Philips SHM6110U Headphone

Search will then fetch following result.

[
    {
        "position": 12,
        "link": "http:\/\/www.talash.com\/buy-online-philips-shl5001-hi-fi-headphone-headband-headphone-india-product.html",
        "image": "http:\/\/staticus.talash.com\/product_images\/p\/092\/DM1573_1lg__52708_thumb.jpg",
        "prod": "Philips Shl5001 Hi Fi Headphone Headband Headphone",
        "price": "Rs. 1000"
    },
    {
        "position": 12,
        "link": "http:\/\/www.talash.com\/buy-online-philips-shl5000-hi-fi-headphone-headband-headphone-india-product.html",
        "image": "http:\/\/staticus.talash.com\/product_images\/n\/497\/DM1572_1lg__57945_thumb.jpg",
        "prod": "Philips Shl5000 Hi Fi Headphone Headband Headphone",
        "price": "Rs. 1030"
    }
]​

Now, I have to sort them according to the match string "Philips SHM6110U Headphone" and the prod value of JSON.

Suggesting an algorithm or sample source code will do!

3
  • Please include an example of exactly what the source data structure looks like (array of strings? object of keys/strings?). And, you will have to supply info on "how closely they match" is determined. Commented Mar 1, 2012 at 0:41
  • Is it relevant that you get the data as JSON? Do you have problems parsing it? Or what exactly is your problem? Commented Mar 1, 2012 at 0:45
  • What do you mean "JSON object"? Do you mean "array"? (If not, how does the concept of "arrange" work?) Commented Mar 1, 2012 at 0:46

2 Answers 2

2

You can pass a comparison function to Array.prototype.sort.

var results = [
  {"position": 12,
    ...
   "prod": "Philips Shl5001 Hi Fi Headphone Headband Headphone",
    ...
  },
  {"position": 12,
    ...
   "prod": "Philips Shl5000 Hi Fi Headphone Headband Headphone",
    ...
  }
]​;

results.sort(function(a, b) {
  return a.prod < b.prod? -1 : a.prod == b.prod? 0 : 1;
});

The result will be the array sorted by obj.prod values.

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

2 Comments

But, I don't wish to sort the array alphabetically. I just wish to give them a weightage according to how close or apart they are as per the search string
Put whatever logic you like in the compare function to determine the value to return. An equality comparison was a simple example, you can use something more sophisticated if you want.
0

I do it finally using a number of criteria :-

  1. Individual word matching of the search result and search query
  2. Extra credits for complete matching of result and search query
  3. Portion of length of all words matching to the total length of search result
  4. Extra credits if the search query appears at an index less than half of the total length of search result
  5. A price filter for further improvements in result.

Here is the final result

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.