Browse Source

Improve train search sorting

master
Kenneth Bruen 2 years ago
parent
commit
9f229be704
Signed by: kbruen
GPG Key ID: C1980A470C3EE5B1
  1. 18
      train.js

18
train.js

@ -30,14 +30,28 @@ function rebuildSuggestions() {
var trainNumber = trainNumberInput.value.trim() var trainNumber = trainNumberInput.value.trim()
var suggestions = [] var suggestions = []
if (!trainNumber) {
suggestions = knownTrains.slice()
}
else {
for (var i = 0; i < knownTrains.length; i++) { for (var i = 0; i < knownTrains.length; i++) {
if (trainNumber) {
if (!knownTrains[i].number.includes(trainNumber)) { if (!knownTrains[i].number.includes(trainNumber)) {
continue continue
} }
}
suggestions.push(knownTrains[i]) suggestions.push(knownTrains[i])
} }
suggestions.sort((s1, s2) => {
if (s1.number.indexOf(trainNumber) != s2.number.indexOf(trainNumber)) {
return s1.number.indexOf(trainNumber) - s2.number.indexOf(trainNumber);
}
if (s1.number.length != s2.number.length) {
return s1.number.length - s2.number.length;
}
return s1.number.localeCompare(s2.number);
})
}
suggestions.forEach(function (suggestion, index) { suggestions.forEach(function (suggestion, index) {
var suggestionLi = document.createElement('li') var suggestionLi = document.createElement('li')

Loading…
Cancel
Save