Loading

js通用对象数组冒泡排序

数组对象通用

  function sort(data, sortFiled, orderby) {
            var result = data,
                temp;

            for (var i = 0; i < result.length; i++) {
                for (var j = 0; j < i; j++) {
                    var resultfiled_i = result[i][sortFiled] || 0,
                         resultfiled_j = result[j][sortFiled] || 0;
                    if (orderby === 'desc') {
                        if (parseInt(resultfiled_i) > parseInt(resultfiled_j)) {
                            temp = result[j];
                            result[j] = result[i];
                            result[i] = temp;
                        }
                    } else {
                        if (parseInt(resultfiled_i) < parseInt(resultfiled_j)) {
                            temp = result[j];
                            result[j] = result[i];
                            result[i] = temp;
                        }
                    }
                }
            }
            return result;
        }



 

posted @ 2016-03-14 16:26  myEsn2E9  阅读(376)  评论(0编辑  收藏  举报