sort search results
This commit is contained in:
parent
9768df803f
commit
09ec6f507c
1 changed files with 29 additions and 3 deletions
|
@ -1,8 +1,8 @@
|
|||
<div class="search">
|
||||
<div class="search__main">
|
||||
<form id="search_form">
|
||||
<label>Test field: <input type="text"></label>
|
||||
<button type="submit">Submit form</button>
|
||||
<label>Wat zoek je? <input type="text"></label>
|
||||
<button type="submit">Zoek!</button>
|
||||
</form>
|
||||
</div>
|
||||
<div id="search_wrapper" class="search__result"></div>
|
||||
|
@ -52,6 +52,32 @@ function resetFromUrl() {
|
|||
}
|
||||
|
||||
function ready() {
|
||||
Array.prototype.stableSort = function(cmp) {
|
||||
cmp = !!cmp ? cmp : (a, b) => {
|
||||
if (a < b) return -1;
|
||||
if (a > b) return 1;
|
||||
return 0;
|
||||
};
|
||||
let stabilizedThis = this.map((el, index) => [el, index]);
|
||||
let stableCmp = (a, b) => {
|
||||
let order = cmp(a[0], b[0]);
|
||||
if (order != 0) return order;
|
||||
return a[1] - b[1];
|
||||
}
|
||||
stabilizedThis.sort(stableCmp);
|
||||
for (let i=0; i<this.length; i++) {
|
||||
this[i] = stabilizedThis[i][0];
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
Array.prototype.sortByKey = function(key) {
|
||||
return this.sort(function(a, b) {
|
||||
var x = key(a); var y = key(b);
|
||||
return ((x < y) ? -1 : ((x > y) ? 1 : 0));
|
||||
});
|
||||
}
|
||||
|
||||
function render_results(div, partials) {
|
||||
const create_wrapper= document.createElement('div');
|
||||
for(let partial of partials) {
|
||||
|
@ -92,7 +118,7 @@ function ready() {
|
|||
|
||||
doSearch = (query) => {
|
||||
clearDiv(wrapper);
|
||||
render_results(wrapper, index.search(query).map(i => partials[i.ref]));
|
||||
render_results(wrapper, index.search(query).sortByKey(i => partials[i.ref].titel).stableSort((a, b) => b.score - a.score).map(i => partials[i.ref]));
|
||||
|
||||
window.history.pushState("search state", "", window.location.pathname + "?q="+query);
|
||||
};
|
||||
|
|
Loading…
Reference in a new issue