//json对象数组按对象属性排序 function JsonSort(obj, field, sortby) { this.obj = obj; this.field = field; this.sortby = sortby; } JsonSort.prototype.sort = function () { var $this = this; var ascend = function (a, b) { return parseInt(a[$this.field]) > parseInt(b[$this.field]) ? 1 : -1; }; var descend = function (a, b) { return parseInt(a[$this.field]) > parseInt(b[$this.field]) ? -1 : 1; }; if (this.sortby == "ascend") { this.obj.sort(ascend); } else { this.obj.sort(descend); } };
var jsonSort = new JsonSort(productJsonList, 'sortindex', 'ascend');
jsonSort.sort();