【发布时间】:2012-03-19 02:02:33
【问题描述】:
我正在编写一个基于 javascript 的过滤器:
有很多类 .single 的 div 包含多个文本元素。过滤器已经隐藏了所有不包含单词的.single:
//take them off dom, manip and put them back
singles.detach().each(function(i) {
var $this = $(this);
//make sure everything is visible
$this.show();
//if this template is not included OR if this index number does not meet the treshold
if($.inArray(i, included) == -1 || treshold >= templates[i].score) {
$this.hide();
}
}).appendTo(container);
数组included 包含.single 的所有索引,其中至少包含一个单词。数组templates 包含具有每个.single 的所有文本的对象以及基于相关性和字数的分数。例如:
templates = [
{ text: ['long string', 'long string 2'], score: 5 },
{ text: ['sf sd', 'gdasd'], score: 3 }
]
(因此所有索引都不会发生在 DOM 中)
现在我想要的是根据这个分数对集合进行排序,或者换句话说:最高分在顶部。集合中每个元素的索引与templates 中的索引相同(包含所有分数)。
【问题讨论】:
标签: javascript jquery sorting filter set