diff --git a/editor/js/ui/typeSearch.js b/editor/js/ui/typeSearch.js index e3cb7f1ee..3b068a96a 100644 --- a/editor/js/ui/typeSearch.js +++ b/editor/js/ui/typeSearch.js @@ -19,6 +19,7 @@ RED.typeSearch = (function() { function search(val) { activeFilter = val.toLowerCase(); var visible = searchResults.editableList('filter'); + searchResults.editableList('sort'); setTimeout(function() { selected = 0; searchResults.children().removeClass('selected'); @@ -101,6 +102,23 @@ RED.typeSearch = (function() { } return (activeFilter==="")||(data.index.indexOf(activeFilter) > -1); }, + sort: function(A,B) { + if (activeFilter === "") { + return A.i - B.i; + } + var Ai = A.index.indexOf(activeFilter); + var Bi = B.index.indexOf(activeFilter); + if (Ai === -1) { + return 1; + } + if (Bi === -1) { + return -1; + } + if (Ai === Bi) { + return sortTypeLabels(A,B); + } + return Ai-Bi; + }, addItem: function(container,i,object) { var def = object.def; object.index = object.type.toLowerCase(); @@ -225,7 +243,17 @@ RED.typeSearch = (function() { } return label; } - + function sortTypeLabels(a,b) { + var al = a.label.toLowerCase(); + var bl = b.label.toLowerCase(); + if (al < bl) { + return -1; + } else if (al === bl) { + return 0; + } else { + return 1; + } + } function refreshTypeList() { var i; searchResults.editableList('empty'); @@ -250,27 +278,19 @@ RED.typeSearch = (function() { items.push({type:t,def: def, label:getTypeLabel(t,def)}); } }); - items.sort(function(a,b) { - var al = a.label.toLowerCase(); - var bl = b.label.toLowerCase(); - if (al < bl) { - return -1; - } else if (al === bl) { - return 0; - } else { - return 1; - } - }) + items.sort(sortTypeLabels); var commonCount = 0; var item; + var index = 0; for(i=0;i