Browse Source

Improve train search sorting

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

20
train.js

@ -30,13 +30,27 @@ function rebuildSuggestions() {
var trainNumber = trainNumberInput.value.trim()
var suggestions = []
for (var i = 0; i < knownTrains.length; i++) {
if (trainNumber) {
if (!trainNumber) {
suggestions = knownTrains.slice()
}
else {
for (var i = 0; i < knownTrains.length; i++) {
if (!knownTrains[i].number.includes(trainNumber)) {
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) {

Loading…
Cancel
Save