admin管理员组文章数量:1312941
I have an array containing results form a geolocation code. I want to sort it by the closest match to the term that I have searched.
Example. Search: Pizza
.
Array: Pizza Uno, Pizzeria Uno, Burgers and Pizzeria, Cino Pizzeria.
The sorted array should be:
Pizza Uno,
Pizzeria Uno,
Burgers and Pizzeria,
Cino Pizzeria.
Thanks for your help.
I have an array containing results form a geolocation code. I want to sort it by the closest match to the term that I have searched.
Example. Search: Pizza
.
Array: Pizza Uno, Pizzeria Uno, Burgers and Pizzeria, Cino Pizzeria.
The sorted array should be:
Pizza Uno,
Pizzeria Uno,
Burgers and Pizzeria,
Cino Pizzeria.
Thanks for your help.
Share Improve this question edited Dec 9, 2011 at 20:36 Michael Berkowski 271k47 gold badges450 silver badges393 bronze badges asked Dec 9, 2011 at 20:35 Leonardo AmigoniLeonardo Amigoni 2,3275 gold badges25 silver badges37 bronze badges 3- 1 You'll have to describe the algorithm you want to use for determining "closest"-ness. Obviously a perfect match can be highest, but what determines the order of the other three items that all have the same partial match? – jfriend00 Commented Dec 9, 2011 at 20:37
- 1 Why is "Burgers and Pizzeria" a closer match to "Pizza" than "Cino Pizzeria"? – Mark Byers Commented Dec 9, 2011 at 20:38
- Good questions. I don't know what the best algorithm would be. I was thinking of matching the search string and then displaying the ones with same match by alphabetical order. Hence why Burgers and Pizzeria is closer then Cino Pizzeria. Same match but higher in alphabetical – Leonardo Amigoni Commented Dec 9, 2011 at 21:09
3 Answers
Reset to default 3A really basic algorithm that'd work, would be to sort based on what percentage of the total string length, the match takes up. So an exact match of "Pizza" would be 5/5 (100%), the "Pizza Uno" match would be 5/9, "Pizzeria Uno" is 4/12, and so on. This is one of the core ponents of the MySQL natural sorting algorithm at its most basic.
What about something like this? It was my first approach for getting the closest color name, depending on a hex-color.
There's of course a better solution out there, you could for example take a look at the sift
algorithm which is really much faster than levenshtein's approach.
However this should work for you as expected.
Array.closest = (function () {
function levenshtein(s, t) {
if (!s.length) return t.length;
if (!t.length) return s.length;
return Math.min(
levenshtein((s.substring(1), t) + 1,
levenshtein((t.substring(1), s) + 1,
levenshtein((s.substring(1), t.substring(1)) + (s[0] !== t[0] ? 1 : 0)
);
}
return function (arr, str) {
return arr.sort(function (a, b) {
return levenshtein((a, str) - levenshtein((b, str);
});
};
}());
var arr = ['Pizza Uno', 'Pizzeria Uno', 'Burgers and Pizzeria', 'Cino Pizzeria.'];
Array.closest(arr, 'Pizza') // => ['Pizza Uno', 'Pizzeria Uno', 'Cino Pizzeria.', 'Burgers and Pizzeria'];
You can try calculating the Levenshtein Distance between 2 strings. It's basically the number of steps it will take to make the two strings identical.
本文标签: sortingJavascript sort an array by matching to stringStack Overflow
版权声明:本文标题:sorting - Javascript sort an array by matching to string - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741897432a2403661.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论