I am using the following code to sort an array in javascript:
array.sort(function(a,b){
var score_a = $.fuzzyMatch(a['name'], user).score,
score_b = $.fuzzyMatch(b['name'], user).score;
return score_a < score_b ? -1 : score_a === score_b ? 0 : 1;
});
The sorting is taking the ‘name’ property of each row in the array and running it through my fuzzymatch function and returning a score so that most relevant matches are returned at the top of the array.
However I wish to include another property in the sort which in this case will be a['folcode'] or b['folcode']
So to sumarise, how can I modify my code to always put rows that contain ['folcode'] == 4 at the top, sorted by best ['name'] match, then return ['folcode'] == 3 rows, also sorted by best name match… etc…
If you want to sort by
folcodeprimarily, and bynamewithin eachfolcode, you would have to yield a comparison forfolcodejust as the one you’re currently doing forname, but return the value of the name comparison only in the equals condition.