Javascript jQuery按评级排序(desc)和基于值隐藏div
我正在努力实现以下目标。我有一打类似于:Javascript jQuery按评级排序(desc)和基于值隐藏div,javascript,jquery,logic,Javascript,Jquery,Logic,我正在努力实现以下目标。我有一打类似于: <div id=1><div>test 1</div><div>4</div></div> <div id=2><div>test2</div><div>1</div></div> <div id=3><div>test3</div><div>6</d
<div id=1><div>test 1</div><div>4</div></div>
<div id=2><div>test2</div><div>1</div></div>
<div id=3><div>test3</div><div>6</div></div>
<div id=4><div>test4</div><div>3</div></div>
<div id=5><div>test5</div><div>2</div></div>
<div id=6><div>test6</div><div>0</div></div>
<div id=7><div>test7</div><div>3</div></div>
...
测试14
测试21
测试36
测试43
测试52
测试60
测试73
...
现在我想使用jQuery只显示排名前5位的div,即按该顺序排列的4,6,3,2,3,并隐藏其余的div
你知道怎么做吗?我不希望使用任何额外的插件等。为您的HTML提供更多的结构,以便您可以将其与选择器一起使用:
<div class="song">
<div>4</div>
</div>
<div class="song">
<div>2</div>
</div>
<div class="song">
<div>6</div>
</div>
<div class="song">
<div>3</div>
</div>
<div class="song">
<div>1</div>
</div>
<div class="song">
<div>1</div>
</div>
<div class="song">
<div>0</div>
</div>
O(n*m)
O(n)
很确定这是不可能的
结果以毫秒为单位
(Mac OS X 10.6、Safari 4.0.3、2.4 Ghz Intel Core 2 Duo)
-------
10个要素
n*logn:3
男:2
m*logn:2
100个元素
n*logn:26
男:11
m*logn:5
1000个元素
n*logn:505
男:140
m*logn:42
万元
n*logn:8016
男:1648
m*logn:442
这是我编造的一个快速破解,所以如果结果是死亡,我不承担责任
var divs = $('div[id] div:contains("test") + div').get();
divs = divs.sort(function( a, b ) {
return ( parseInt( b.textContent, 10 ) || -1 ) - ( parseInt( a.textContent, 10 ) || -1 );
}).slice(5);
$( divs ).hide();
但是我同意更多的HTML结构肯定会让你的工作更容易…你只是想显示前5名的内容吗?或者你想根据他们的内容显示前五名吗?当然,他们的内容会有所不同。但根据收视率,我想隐藏所有不是前5名的div。请注意,前5名并不意味着前5名。它们不会以任何方式排序?就像你现在给他们看的:4,2,6,4,1,1,0。如果有一个内容为7的8个div,那么该div也需要显示吗?有道理吗?是的,对。。。对不起,如果我的问题不太清楚。它应该基本上隐藏其余的不是排名前五的问题。根据我的要求进行调整。很像一个符咒。如果我有任何问题,我会告诉你。。。非常感谢。。。还有比这更有效的吗?对不起。。。完成。。。。大多数人会接受这个答案。。。想知道这是否可以更有效地完成?我刚刚添加了一个O(n*m)方法,但请注意,对于小型数据集,内置的
sort()
方法可能更快。使用堆可以创建一个O(m*logn)
的解决方案,这是您可以获得的最佳运行时。非常全面。非常感谢你。我一直认为堆排序只是为了学术目的:)
songs = $("div.song").get();
for (var i = 0; i < 5; i++) {
var indexOfTop = -1;
var topRating = -1;
// find best rated song
jQuery.each(songs, function(j) {
// this line needs to be adapted for your code
var rating = $(this).text();
if (rating > topRating) {
topRating = rating;
indexOfTop = j;
}
});
// remove top item from array
if (indexOfTop == -1) {
// no items left in songs
return false;
} else {
songs.splice(indexOfTop, 1);
}
}
// remove remaining songs
$(songs).hide();
function BinaryHeap(keyFunction) {
if (arguments.length >= 1) {
this.keyFunction = keyFunction;
}
this.content = [];
}
BinaryHeap.buildHeap = function(items, keyFunction) {
var newHeap = new BinaryHeap();
if (arguments.length >= 2) {
this.keyFunction = keyFunction;
}
// slice copies the array
newHeap.content = items.slice(0);
var firstParent = Math.floor((newHeap.content.length - 1) / 2);
for (var i = firstParent; i >= 0; i--) {
newHeap._siftDown(i);
}
return newHeap;
}
BinaryHeap.prototype = {
push: function(item) {
this.content.push(item)
this._siftUp(this.content.length - 1);
},
pop: function() {
var value = this.content[0];
var newHead = this.content.pop();
if (this.content.length >= 1) {
this.content[0] = newHead;
this._siftDown(0);
}
return value;
},
// default key function, it extracts a key from the object
keyFunction: function(a) {
return a;
},
_siftDown: function(root) {
var length = this.content.length;
var k = 0;
while (root * 2 + 1 < length) {
k++;
var child = root * 2 + 1;
var rightChild = root * 2 + 2;
if (rightChild < length) {
child = this._max(child, rightChild);
}
if (this._max(root, child) == child) {
this._swap(root, child);
} else {
break;
}
root = child;
}
},
_siftUp: function(child) {
while (child >= 0) {
var root = Math.floor((child - 1) / 2);
if (this._max(child, root) == root) {
this._swap(child, root);
} else {
return;
}
child = root;
}
},
_max: function(a, b) {
return (this.keyFunction(this.content[a]) >= this.keyFunction(this.content[b])) ? a : b;
},
_swap: function(a, b) {
var buffer = this.content[a];
this.content[a] = this.content[b];
this.content[b] = buffer;
}
}
allSongs = $("div.song");
// build heap in O(n)
var myheap = BinaryHeap.buildHeap(allSongs.get(), function(item) {
return $(item).text();
});
// hide all items
allSongs.hide();
// show top 5
for (var i = 0; i < 5; i++) {
var item = myheap.pop();
// less than 5 elements
if (typeof(item) == "undefined")
break;
$(item).show();
}
Results in Milliseconds
(Mac OS X 10.6, Safari 4.0.3, 2.4 Ghz Intel Core 2 Duo)
-------
10 elements
n*logn: 3
m*n: 2
m*logn: 2
100 elements
n*logn: 26
m*n: 11
m*logn: 5
1000 elements
n*logn: 505
m*n: 140
m*logn: 42
10000 elements
n*logn: 8016
m*n: 1648
m*logn: 442
var divs = $('div[id] div:contains("test") + div').get();
divs = divs.sort(function( a, b ) {
return ( parseInt( b.textContent, 10 ) || -1 ) - ( parseInt( a.textContent, 10 ) || -1 );
}).slice(5);
$( divs ).hide();