理由はわかりませんが、このコードは私のliを適切にソートしていません。順番が入れ替わっていますが、例えば 1, 5, 2, 9 だったら 5, 2, 9, 1 と変わっています。
jQuery コード:
var sorter_type=new Array("date_sort","price_sort","discount_sort","time_sort");
function sorting_by_filter(filter_id,field)
{
var sort_type="";
if($('#'+filter_id).attr("class")=="asc")
{
$('#'+filter_id).removeClass("asc");
$('#'+filter_id).addClass("desc");
sort_type=" desending";
}
else
{
$('#'+filter_id).removeClass("desc");
$('#'+filter_id).addClass("asc");
sort_type="";
}
for(i=0;i<sorter_type.length;i++)
{
if(sorter_type[i]==filter_id)
{
$('#'+filter_id).parent().attr("class","");
$('#'+filter_id).parent().addClass("clsTop_Menu_Act"+sort_type);
}
else
{
$("#"+sorter_type[i]).parent().attr("class","");
}
}
var $sort = $('#'+filter_id);
var $list = $('.clsDeal_Whole_Cont');
var $listLi = $('li.clsDeal_Blk_Cont',$list);
$listLi.sort(function(a, b){
var keyA = parseInt($(a).attr(field));
var keyB = parseInt($(b).attr(field));
if($($sort).hasClass('asc')){
return (keyA > keyB) ? 1 : 0;
} else {
return (keyA < keyB) ? 1 : 0;
}
});
$.each($listLi, function(index, row){
$list.append(row);
});
}
ボタンコード:
<ul class="clearfix" id="filters">
<li class="clsTop_Menu_Act"><a href="javascript:void(0)" class="asc" onclick="sorting_by_filter('date_sort','title')" id="date_sort">New</a></li>
<li><a href="javascript:void(0)" onclick="sorting_by_filter('price_sort','price')" class="asc" id="price_sort">Price</a></li>
<li><a href="javascript:void(0)" onclick="sorting_by_filter('discount_sort','discount')" class="asc" id="discount_sort">Discount</a></li>
<li><a href="javascript:void(0)" onclick="sorting_by_filter('time_sort','time_left')" class="asc" id="time_sort" >Time Left</a></li>
</ul>
ソートが必要なコンテンツ:
<ul class="clsDeal_Whole_Cont clearfix">
<li class="clsDeal_Blk_Cont" style="display:block" id="deal_1" price="2" discount="50" time_left="1360363800">
<li class="clsDeal_Blk_Cont" style="display:block" id="deal_10" price="20" discount="45" time_left="1360363700">
<li class="clsDeal_Blk_Cont" style="display:block" id="deal_5" price="19" discount="80" time_left="1360363800">
</ul>