javascript技巧

关注公众号 jb51net

关闭
首页 > 网络编程 > JavaScript > javascript技巧 > js对json数组进行排序

js中如何对json数组进行排序

作者:spring-java

这篇文章主要介绍了js中如何对json数组进行排序的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

js对json数组进行排序

废话不多说,直接上代码

// 升序排列
function up(a, b) {
     return a.val-b.val
 },
 // 降序排列
function down(a, b) {
    return b.val-a.val
},

// sort 会直接对原数据排序
testJson.sort(up)

例如:

let test=[{"val":100},{"val":70},{"val":50},{"val":20},{"val":10}]
test=test.sort(up)

js根据json数组多个字段排序

let newArray = [
      {product: "1", matno: "420200011480006",salebillno:"xsdd2733-8", sales: "800"},
      {product: "2", matno: "420200011480007",salebillno:"xsdd2733-2", sales: "300"},
      {product: "3", matno: "420200011480006",salebillno:"xsdd2733-5", sales: "100"},
      {product: "4", matno: "420200011480006",salebillno:"xsdd2733-3", sales: "200"},
      {product: "5", matno: "420200011480008",salebillno:"xsdd2738", sales: "600"},
      {product: "6", matno: "420200011480010",salebillno:"xsdd2734", sales: "400"},
      {product: "7", matno: "420200011480009",salebillno:"xsdd2735", sales: "700"},
      {product: "8", matno: "420200011480001",salebillno:"xsdd2737", sales: "500"}
     ]
     
 
/**数组根据数组对象中的某个属性值进行排序的方法 
  * 使用例子:newArray.sort(sortByArr(['matno'],true)) //表示根据matno属性降序排列;若第二个参数不传递,默认表示升序排序
  * @param attr 排序的属性 ['matno','salebillno'...],根据一个字段或者多个字段排序
  * @param rev true表示升序排列,false降序排序
  * */
 
function sortByArr(arr, rev) {
 if (rev == undefined) {
 rev = 1;
 } else {
 rev = (rev) ? 1 : -1;
 }
 return function(a, b) {
 for (var i = 0; i < arr.length; i++) {
 let attr = arr[i]
 if (a[attr] != b[attr]) {
 if (a[attr] > b[attr]) {
 return rev * 1;
 } else {
 return rev * -1;
 }
 }
 }
}
}
/**升序**/
 newArray.sort(sortByArr(['matno','salebillno'],true))
[{product: '8', matno: '420200011480001', salebillno: 'xsdd2737', sales: '500'}
 {product: '4', matno: '420200011480006', salebillno: 'xsdd2733-3', sales: '200'}
 {product: '3', matno: '420200011480006', salebillno: 'xsdd2733-5', sales: '100'}
 {product: '1', matno: '420200011480006', salebillno: 'xsdd2733-8', sales: '800'}
 {product: '2', matno: '420200011480007', salebillno: 'xsdd2733-2', sales: '300'}
 {product: '5', matno: '420200011480008', salebillno: 'xsdd2738', sales: '600'}
 {product: '7', matno: '420200011480009', salebillno: 'xsdd2735', sales: '700'}
 {product: '6', matno: '420200011480010', salebillno: 'xsdd2734', sales: '400'}]
/**降序**/
newArray.sort(sortByArr(['matno'],false))
// 输出
[{product: '6', matno: '420200011480010', salebillno: 'xsdd2734', sales: '400'}
 {product: '7', matno: '420200011480009', salebillno: 'xsdd2735', sales: '700'}
 {product: '5', matno: '420200011480008', salebillno: 'xsdd2738', sales: '600'}
 {product: '2', matno: '420200011480007', salebillno: 'xsdd2733-2', sales: '300'}
 {product: '1', matno: '420200011480006', salebillno: 'xsdd2733-8', sales: '800'}
 {product: '3', matno: '420200011480006', salebillno: 'xsdd2733-5', sales: '100'}
 {product: '4', matno: '420200011480006', salebillno: 'xsdd2733-3', sales: '200'}
 {product: '8', matno: '420200011480001', salebillno: 'xsdd2737', sales: '500'}]

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文