いくつかのDOM要素を持つjQueryオブジェクトがあります。私はこのオブジェクトを取り、その要素を並べ替えます。しかし、いくつかの要素が優先されるので、並べ替えたときに優先順位のない要素の下に置くことはできません。jQueryオブジェクトにいくつかのDOM要素があるかどうかを確認する最速の方法
例:
<!-- original list -->
<el id='2' /> <!-- has priority -->
<el id='0' /> <!-- has priority -->
<el id='3' /> <!-- has priority -->
<el id='1' /> <!-- no priority -->
<el id='4' /> <!-- no priority -->
<!-- wrong way to sort -->
<el id='0' /> <!-- has priority -->
<el id='1' /> <!-- no priority -->
<el id='2' /> <!-- has priority -->
<el id='3' /> <!-- has priority -->
<el id='4' /> <!-- no priority -->
<!-- right way to sort -->
<el id='0' /> <!-- has priority -->
<el id='2' /> <!-- has priority -->
<el id='3' /> <!-- has priority -->
<el id='1' /> <!-- no priority -->
<el id='4' /> <!-- no priority -->
すると、私はこれを解決しています方法:
var elements = $(selector);
elements.sort(sort_by_id);
var priority_elements = get_prio_elements(); //That's the only way to know which are the elements with priority
var temp = $();
elements.each(function(){
if(priority_elements.is(this)) //check if element are inside priority list
temp = temp.add($(this)); //push element in elements object order
});
set_prio_elements(temp);
私の出力はtemp
オブジェクトでなければなりません。つまり、temp
には、以下が含まれている必要があります。
<el id='0' /> <!-- has priority -->
<el id='2' /> <!-- has priority -->
<el id='3' /> <!-- has priority -->
問題は、このコードが大規模なリスト(300+以上)に対して非常に遅いことです。私はこれをどのように改善することができるか知りたい。
A link at fiddle that could help understand.
'sort_function'はどのように見えますか?同じソート操作で優先順位とIDで注文することができます。 – Pointy
あなたが提供したコードのどれもがかなり遅くなるはずです。 *正確に*どの部分が遅いかを見るためにそれをプロファイリングしましたか?これは、 'sort_function'によるソート、または' get_prio_elements'を介した優先順位要素の取得です。 – Matt
'each()'の部分が遅いです。そして私はセレクターとして["priority = 'true']" 'を使用することはできません、実際には、私は優先順位を示すものは何もありません。私は 'get_prio_elements'関数で優先順位の要素にアクセスできます。 –