javascript - AngularJS: Order search results by similarity to search text -


i have built countless angular-powered, find-as-you-type-style searches on past year or so, , love how easy implement. however, 1 problem i've never found solution how sort search results. appreciate has ideas on how this.

here's problem:

please see this plunker reference

if @ super-simple example plunker above, have list of sample pets want able search through. if type text field, list of pets gets narrowed down. great. problem, search results aren't sorted in relation how similar text entered.

for example:

there total of 8 pets in list, sorted name using orderby:'name' filter on list's ng-repeat directive. if type character t input, 5 remaining results. pet @ top of list "blackie" (because blackie first pet in list has t in it, because blackie's type "cat", contains t).

sample search results

what see way sort remaining results similarity search text. so, in example above, instead of seeing "blackie" @ top of list, see "tom" (which @ bottom of filtered list) because tom's name starts letter t, started searching , more trying find.

any ideas on how implement sort of search algorithm? haven't been able find in of googling , of attempts write such algorithm.

thanks in advance ideas!

define own filter narrow down list , each item calculate rank , assign calculated rank item. order rank.

change html this:

<pre ng-repeat="pet in pets | ranked:inputtext | orderby:'rank':true">{{pet | json}}</pre> 

add following filter:

app.filter('ranked', function() { return function(obj, searchkey) {   if (!obj)     return obj;   for(var =0; i< obj.length; i++)   {      var currentobj = obj[i];       var numofname = currentobj.name.indexof(searchkey) + 1;       var numoftype = currentobj.type.indexof(searchkey) + 1;       var numofcolor = currentobj.color.indexof(searchkey) + 1;        currentobj.rank = numofname * 10 + numoftype * 5 + numofcolor;   }   return obj; } }); 

Comments

Popular posts from this blog

How to access named pipes using JavaScript in Firefox add-on? -

multithreading - OPAL (Open Phone Abstraction Library) Transport not terminated when reattaching thread? -

node.js - req param returns an empty array -