Help with the compare function, at the moment the array is sorted by the first occurrence

<meta charset="UTF-8"> <form method="post" id="form_search" class="search_form"> <input type="text" class="search_form_text" id="gorod" placeholder="Введите город" list="hint" autocomplete="off"> <datalist id="hint"></datalist> <button type="submit" name="search_form_button">GO</button> </form> <script> var mass = [ ['krasnodar','Краснодар'], ['ryazan','Рязань'], ['belgorod','Белгород'], ['rostov','Ростов'], ['voronezh','Воронеж'] ]; var city_list = document.getElementById('gorod'); function search_down(str) { var hint = document.getElementById('hint'); var replacer = { "q": "й", "w": "ц", "e": "у", "r": "к", "t": "е", "y": "н", "u": "г", "i": "ш", "o": "щ", "p": "з", "[": "х", "]": "ъ", "a": "ф", "s": "ы", "d": "в", "f": "а", "g": "п", "h": "р", "j": "о", "k": "л", "l": "д", ";": "ж", "'": "э", "z": "я", "x": "ч", "c": "с", "v": "м", "b": "и", "n": "т", "m": "ь", ",": "б", ".": "ю", "/": "." }; var obj = str.replace(/[Az/,.;\'\]\[]/g, function (x) { return x === x.toLowerCase() ? replacer[x] : replacer[x.toLowerCase()].toUpperCase(); }); var reg = new RegExp('' + obj, 'i'); hint.innerHTML = ''; function compare (a,b) { var nameA = a[1].indexOf(obj.toLowerCase()); var nameB = b[1].indexOf(obj.toLowerCase()); if (nameA < nameB) return -1; if (nameA > nameB) return 1; else return 0; } mass.sort(compare); var j =0; city_list.value = obj; if (obj.length > 0) { for (var i = 0; i < mass.length; i++) { if (reg.test(mass[i][1]) && obj.toLowerCase() !== mass[i][1].toLowerCase().trim() && j < 5) { var opt = document.createElement('option'); opt.innerHTML = mass[i][1]; hint.appendChild(opt); j++; } } } } var form_search = document.getElementById('form_search'); form_search.onsubmit = function sub() { for(var j=0;j<mass.length;j++) { var reg = new RegExp('^' + mass[j][1].trim() + '$', 'i'); if (reg.test(city_list.value)) { var res = mass[j][0]; } } if(res) { form_search.action = res; return true; } else { city_list.value = ""; document.getElementById('search_hint').classList.add('search_hint_active'); return false; } }; city_list.addEventListener('keyup',function () {search_down(city_list.value)}); </script> 

Closed due to the fact that off-topic participants 0xdb , Air , aleksandr barakin , Jarvis_J , Kosta B. 10 Dec '18 at 22:12 .

It seems that this question does not correspond to the subject of the site. Those who voted to close it indicated the following reason:

  • “Questions asking for help with debugging (“ why does this code not work? ”) Should include the desired behavior, a specific problem or error, and a minimum code for playing it right in the question . Questions without an explicit description of the problem are useless for other visitors. See How to create minimal, self-sufficient and reproducible example . " - 0xdb, Air, aleksandr barakin, Jarvis_J, Kosta B.
If the question can be reformulated according to the rules set out in the certificate , edit it .

    1 answer 1

    I did not invent anything smarter, it works)

     function compare(a,b) { var res = 0; var j = a[1].toLowerCase().indexOf(obj); var js = b[1].toLowerCase().indexOf(obj); if (j === -1) j = 100; if (js === -1) js = 100; if (j < js) res = -1; else if (j > js) res = 1; else if (j === js) { if (a < b) res = -1; else if (a > b) res = 1; else res = 0; } else res = 0; return res; }