Skip to content
This repository was archived by the owner on Nov 10, 2020. It is now read-only.

Commit 2addd83

Browse files
committed
Refactor search sorting.
1 parent 4aff5ed commit 2addd83

File tree

1 file changed

+34
-36
lines changed

1 file changed

+34
-36
lines changed

_assets/js/swift-site.js

Lines changed: 34 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -205,49 +205,47 @@ $(function() {
205205
selectdata.push({ id: item.toLowerCase(), fullText: item, text: item.toLowerCase(), kind: match[1] });
206206
}
207207
}
208+
208209
$('.select2').select2({
209210
placeholder: "Search",
210211
minimumInputLength: 1,
211212
formatInputTooShort: '',
212213
data: selectdata,
213214
formatResult: format,
214-
sortResults: function(results, container, query) {
215-
if (query.term) {
216-
var term = query.term.toLowerCase();
217-
return results.sort(function(a, b) {
218-
// we sort first by location of the search term in the matches
219-
// that is, when the term is "string", String should match before StaticString
220-
// to begin, find the relative search term offset of the two matches
221-
var aComp = a.text || a.id;
222-
var bComp = b.text || b.id;
223-
var indexOffset = aComp.indexOf(term) - bComp.indexOf(term);
224-
225-
// if the relative offset of the search term is nonzero, the term
226-
// with the lower offset should come first
227-
if (indexOffset != 0) {
228-
return indexOffset;
229-
230-
// otherwise, order using case-insensitive alpha
231-
} else {
232-
if (aComp != bComp) {
233-
return (aComp < bComp) ? -1 : 1;
234-
} else {
235-
// if the two terms are the same, expand the comparison to look at their fully qualified names
236-
indexOffset = a.id.indexOf(term) - b.id.indexOf(term);
237-
if (indexOffset != 0) {
238-
return indexOffset;
239-
} else {
240-
if (a.id == b.id) {
241-
return 0;
242-
} else {
243-
return (a.id < b.id) ? -1 : 1;
244-
}
245-
}
246-
}
247-
}
248-
});
215+
sortResults: function(results, container, query) {
216+
if (!query.term) {
217+
return results;
218+
}
219+
220+
var term = query.term.toLowerCase();
221+
222+
// order two items by the location of the search term in the text
223+
// that is, when the term is "string", String should match before StaticString
224+
var orderByOffset = function(a, b) {
225+
var indexOffset = a.indexOf(term) - a.indexOf(term);
226+
227+
// if the relative offset of the search term is nonzero, the term
228+
// with the lower offset should come first
229+
if (indexOffset != 0) {
230+
return indexOffset;
231+
}
232+
233+
// otherwise, order using case-insensitive alpha
234+
if (a == b) {
235+
return 0;
236+
} else {
237+
return (a < b) ? -1 : 1;
238+
}
249239
}
250-
return results;
240+
241+
return results.sort(function(a, b) {
242+
var compResult = orderByOffset(a.text || a.id, b.text || b.id);
243+
if (compResult != 0) {
244+
return compResult;
245+
}
246+
247+
return orderByOffset(a.id, b.id);
248+
});
251249
}
252250
})
253251
.on("change", function(e) {

0 commit comments

Comments
 (0)