js中怎么对json数组进行排序

寻技术 JS脚本 2023年08月15日 98

今天小编给大家分享一下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'}]
关闭

用微信“扫一扫”