count size array按顺序显示为S到L.

时间:2015-11-12 04:39:38

标签: jquery

function checkmysize(){
    var values = [];
    var total = {};
    var total_html ="";
    var inputs = $('.size-col');
    var total_content = $('#size-count-content');

    for(var i = 0; i < inputs.length; i++){
        values.push($(inputs[i]).val());
    }
    for (var j = 0, v = values.length; j < v; j++) {
        total[values[j]] = (total[values[j]] || 0) + 1;
    }
    total_html += "<div id='total-list'><p>"+JSON.stringify(total)+"</p></div>";
    $('#size-count-content').append(total_html);
}

我正在进行计数大小检查,所以基本是用户选择他们需要的所有大小并显示他们选择的总大小,但我希望按顺序显示大小,从“S,M,L”开始但是我的数组显示它是混合的,如何使我的数组按顺序显示它。

点击这里了解更多,谢谢。改变选项 Demo

1 个答案:

答案 0 :(得分:0)

window.onload = function() {
{
var values = ['M','L','S']; //your array which needs sorting
//sorting reference array
var sizes = { 
       "S": 0, "M": 1, "L": 2
    };
    var compareA;
    var compareB;

values.sort(function(a,b){  
 compareA = sizes[a];
 compareB = sizes[b];

 return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
  });
console.log(values);

};