diff --git a/index.js b/index.js index 93c900c..5039673 100644 --- a/index.js +++ b/index.js @@ -161,8 +161,12 @@ const fallbacks = [ { either: isSmallLocality, resolve: resolveSmallLocality }, { either: isSmallLocaladmin, resolve: resolveSmallLocaladmin }, { either: isNonPopularNeighbourhood, resolve: resolveNonPopularNeighbourhood }, - // the 'else' case, always returns true - { either: _.constant(true), resolve: (result1, result2) => { return result1 < result2; } } + // the 'else' case, always sort by score, avoiding returning 0 because js sort is unstable + { either: _.constant(true), resolve: (result1, result2) => { + if( isNaN( result1._score ) || isNaN( result2._score ) ){ return -1; } + if( result1._score === result2._score ){ return -1; } + return result1._score > result2._score ? -1 : 1; + }} ]; module.exports = (clean) => { diff --git a/test/index.js b/test/index.js index 083c344..467c126 100644 --- a/test/index.js +++ b/test/index.js @@ -342,4 +342,21 @@ tape('neighbourhood', (test) => { }); + test.test('custom layers should be sorted by score', t => { + const sorter = require('../index')({}); + + const result_one = new ResultBuilder('stops').score(27.178297).build(); + const result_two = new ResultBuilder('stops').score(1.4754213).build(); + const result_three = new ResultBuilder('stops').score(1.4754213).build(); + + t.equals(sorter(result_one, result_two), -1); + t.equals(sorter(result_two, result_one), +1); + t.equals(sorter(result_two, result_three), -1); + t.equals(sorter(result_three, result_two), -1); + t.equals(sorter(result_one, result_three), -1); + t.equals(sorter(result_three, result_one), +1); + t.end(); + + }); + });