-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiebreakingsort.js
39 lines (35 loc) · 1.11 KB
/
tiebreakingsort.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
const tieBreakingSort = (hitA, hitB) => {
// Default Algolia ranking formula (textual + geo + custom ranking)
const criteria = [
'nbTypos:asc',
'geoDistance:asc',
'words:desc',
'filters:desc',
'proximityDistance:asc',
'firstMatchedWord:asc', // attribute
'nbExactWords:desc',
'userScore:desc' // custom ranking
]
let rank = 0
for (let i = 0; i < criteria.length; ++i) {
const criterion = criteria[i].split(':')
const criterionName = criterion[0]
const criterionOrder = criterion[1]
if (hitA._rankingInfo[criterionName] === hitB._rankingInfo[criterionName]) {
continue
} else if (criterionOrder === 'asc') {
rank = hitA._rankingInfo[criterionName] - hitB._rankingInfo[criterionName]
break
} else if (criterionOrder === 'desc') {
rank = hitB._rankingInfo[criterionName] - hitA._rankingInfo[criterionName]
break
}
}
if (rank === 0) {
// no tie-break, using objectID (higher is better, so 'B' > 'A' and '2' > '1')
return hitA.objectID > hitB.objectID ? 1 : -1
} else {
return rank
}
}
module.exports = tieBreakingSort