In my project we are displaying a HUGE list of strings (17,000+ sometimes) With an input box for pairing down the list using a filter. I've been using angular's default filter until it was requested that i sort so that strings starting with the search query come before words that merely contain it.
Say i have an array of strings
$scope.array = ["aaa","abb","abc","bad","bcd","cool","cold"]
Right now if i filter with the query string 'c'
, what will show up is 'abc','bcd,'cool','cold'
in that order. However, when most people start searching with 'c' they probably want to see 'cool' and 'cold' show up first. I would like the array to be 'cool','cold','abc',bcd
(or possibly cold then cool if alphabetical sorting is part of the package) I'm wary of writing my own filter because I'd rather use something tried and true with such a large collection of data.
Is there a way to achieve this without writing my own custom filter?
It seems like common enough functionality that there should be a config for it or possibly a common filter i can grab from github?
I would think using the angular orderBy with a function that checks if the string starts with C and returns the correct asc value it would do what you need. The question is what would it do after it sorts by the starting characters, does it return the current ordinality of the array, dont know if that even matters for you.
Some pseudo:
$scope.vm.filter = 'c'
$scope.orderByStartsWith = function(item) {
return item.startsWith($scope.vm.filter) ? 0 : 1;
}
<li ng-repeat="item in array | filter:vm.filter | orderBy:orderByStartsWith" >{{item}}</li>
I wish angular would allow you to pass more params into the order by function.