对JSON数组的结果进行排序的最佳方法?我需要先按customer
然后按invoice
进行重新排序,以使所有客户在一起,并以递增的invoice
值。与SQL相同的是:SELECT * FROM Table ORDER BY customer, invoice ASC
。这是尝试:http://jsfiddle.net/ramd6u83/
var values = [];
for (var invoice in json) {
values.push(json[invoice]);
}
var str = values.sort().join('\n');
console.log(str);
var json = [{
"autonum": "26",
"date": "2017-04-17",
"customer": "jad Rasul\r\n",
"address": "Washington, DC 20017",
"invoice": "20170412",
"sales": "450.00",
"paid": "0.00",
"owed": "450.00",
"tax": "0.00",
"misc": "2017-04-17 15:35:08",
"cust_id": "16",
"product": "Hourly Rate",
"description": "Business Rate: Consulting/Labor/Installs",
"cost": "150.00",
"quantity": "3",
"price": "450.00",
"timestamp": "2017-04-17 15:35:08"
},
{
"autonum": "27",
"date": "2017-07-31",
"customer": "jad Rasul\r\n",
"address": "Washington, DC 20017",
"invoice": "20170731",
"sales": "300.00",
"paid": "0.00",
"owed": "300.00",
"tax": "0.00",
"misc": "2017-07-31 14:19:02",
"cust_id": "19",
"product": "Hourly Rate",
"description": "Business Rate: Consulting/Labor/Installs",
"cost": "150.00",
"quantity": "2",
"price": "300.00",
"timestamp": "2017-08-01 17:19:02"
},
{
"autonum": "31",
"date": "2018-06-02",
"customer": "jad Rasul\r\n",
"address": "1160 Varnum Street\r\nSuite 114\r\nWashington, DC 20017\r\n\t\t\t",
"invoice": "20180611",
"sales": "300.00",
"paid": "0.00",
"owed": "300.00",
"tax": "0.00",
"misc": "2018-07-02 15:13:34",
"cust_id": "22",
"product": "Hourly Rate",
"description": "Business Rate: Consulting/Labor/Installs",
"cost": "150.00",
"quantity": "2",
"price": "300.00",
"timestamp": "2018-07-02 15:13:34"
},
{
"autonum": "90",
"date": "2017-02-05",
"customer": "jad Rasul",
"address": "Washington, DC 20017",
"invoice": "20170205",
"sales": "450.00",
"paid": "150.00",
"owed": "300.00",
"tax": "2.00",
"misc": "2017-02-05 20:20:25",
"cust_id": "1",
"product": "Hourly Rate",
"description": "Business Rate: Consulting/Labor/Installs",
"cost": "150.00",
"quantity": "3",
"price": "450.00",
"timestamp": "2019-02-05 02:22:47"
},
{
"autonum": "91",
"date": "2017-02-05",
"customer": "jad Rasul",
"address": "Washington, DC 20017",
"invoice": "20170205",
"sales": "300.00",
"paid": "50.00",
"owed": "250.00",
"tax": "2.00",
"misc": "2017-02-05 20:21:18",
"cust_id": "2",
"product": "Hourly Rate",
"description": "Business Rate: Consulting/Labor/Installs",
"cost": "150.00",
"quantity": "2",
"price": "300.00",
"timestamp": "2019-02-05 02:22:47"
},
{
"autonum": "43",
"date": "2018-11-12",
"customer": "jad Rasul",
"address": "Washington, DC 20017\r\n\t\t\t",
"invoice": "20181024",
"sales": "893.00",
"paid": "300.00",
"owed": "593.00",
"tax": "0.00",
"misc": "2018-11-12 18:32:13",
"cust_id": "27",
"product": "Win10-key",
"description": "OEM Windows 10 Home, 64-Bit, 1-Pack, DVD",
"cost": "98.99",
"quantity": "1",
"price": "98.99",
"timestamp": "2018-11-12 18:32:13"
},
{
"autonum": "89",
"date": "2019-02-25",
"customer": "jad Rasul",
"address": "Washington, DC 20017\r\nSuite 114",
"invoice": "20190225",
"sales": "375.00",
"paid": "0.00",
"owed": "375.00",
"tax": "0.00",
"misc": "2019-02-26 02:22:47",
"cust_id": "56",
"product": "Hourly Rate",
"description": "Commercial Rate - troubleshoot and repair network after Comc",
"cost": "150.00",
"quantity": "3",
"price": "375.00",
"timestamp": "2019-02-26 02:22:47"
},
{
"autonum": "33",
"date": "2018-09-02",
"customer": "Potomac\r\n",
"address": "Damascus, MD 20872\t\t\t",
"invoice": "20180902",
"sales": "467.00",
"paid": "0.00",
"owed": "467.00",
"tax": "0.00",
"misc": "2018-09-02 22:44:09",
"cust_id": "23",
"product": "Hourly Rate",
"description": "Commercial Camera Installation / Mount / Seal",
"cost": "150.00",
"quantity": "2",
"price": "300.00",
"timestamp": "2018-09-02 22:44:09"
},
{
"autonum": "30",
"date": "2017-08-14",
"customer": "Ann Frank\r\n",
"address": "Ellicott City, MD 21043\r\n\r\n\t\t\t",
"invoice": "20170814",
"sales": "350.00",
"paid": "0.00",
"owed": "350.00",
"tax": "0.00",
"misc": "2017-08-14 17:56:52",
"cust_id": "21",
"product": "Hourly Rate",
"description": "Residential Rate: Labor/Installs: 6 Kidde i12010S 10 year sm",
"cost": "350.00",
"quantity": "1",
"price": "350.00",
"timestamp": "2017-08-14 17:56:52"
},
{
"autonum": "21",
"date": "2017-03-22",
"customer": "Bikes Music\r\n",
"address": "Mery Station",
"invoice": "20160929",
"sales": "2416.00",
"paid": "1000.00",
"owed": "1416.00",
"tax": "0.00",
"misc": "2017-03-22 19:51:00",
"cust_id": "15",
"product": "Home Depot",
"description": "Home Depot Supplies for Shelf/mounts/braces/paint.",
"cost": "128.50",
"quantity": "1",
"price": "128.50",
"timestamp": "2017-03-22 19:51:00"
},
{
"autonum": "83",
"date": "2019-02-07",
"customer": "Bikes Music",
"address": "Mery Station",
"invoice": "20190207",
"sales": "95.00",
"paid": "0.00",
"owed": "95.00",
"tax": "0.00",
"misc": "2019-02-15 20:57:54",
"cust_id": "50",
"product": "Hourly Rate",
"description": "Residential Rate: Consulting/Labor/Installs",
"cost": "95.00",
"quantity": "1",
"price": "95.00",
"timestamp": "2019-02-15 20:57:54"
}
];
答案 0 :(得分:2)
您可以通过先使用localeCompare
比较sort
值来使用customer
。如果两者相同,则比较invoice
var json=[{"autonum":"26","date":"2017-04-17","customer":"jad Rasul\r\n","address":"Washington, DC 20017","invoice":"20170412","sales":"450.00","paid":"0.00","owed":"450.00","tax":"0.00","misc":"2017-04-17 15:35:08","cust_id":"16","product":"Hourly Rate","description":"Business Rate: Consulting/Labor/Installs","cost":"150.00","quantity":"3","price":"450.00","timestamp":"2017-04-17 15:35:08"},{"autonum":"27","date":"2017-07-31","customer":"jad Rasul\r\n","address":"Washington, DC 20017","invoice":"20170731","sales":"300.00","paid":"0.00","owed":"300.00","tax":"0.00","misc":"2017-07-31 14:19:02","cust_id":"19","product":"Hourly Rate","description":"Business Rate: Consulting/Labor/Installs","cost":"150.00","quantity":"2","price":"300.00","timestamp":"2017-08-01 17:19:02"},{"autonum":"31","date":"2018-06-02","customer":"jad Rasul\r\n","address":"1160 Varnum Street\r\nSuite 114\r\nWashington, DC 20017\r\n\t\t\t","invoice":"20180611","sales":"300.00","paid":"0.00","owed":"300.00","tax":"0.00","misc":"2018-07-02 15:13:34","cust_id":"22","product":"Hourly Rate","description":"Business Rate: Consulting/Labor/Installs","cost":"150.00","quantity":"2","price":"300.00","timestamp":"2018-07-02 15:13:34"},{"autonum":"90","date":"2017-02-05","customer":"jad Rasul","address":"Washington, DC 20017","invoice":"20170205","sales":"450.00","paid":"150.00","owed":"300.00","tax":"2.00","misc":"2017-02-05 20:20:25","cust_id":"1","product":"Hourly Rate","description":"Business Rate: Consulting/Labor/Installs","cost":"150.00","quantity":"3","price":"450.00","timestamp":"2019-02-05 02:22:47"},{"autonum":"91","date":"2017-02-05","customer":"jad Rasul","address":"Washington, DC 20017","invoice":"20170205","sales":"300.00","paid":"50.00","owed":"250.00","tax":"2.00","misc":"2017-02-05 20:21:18","cust_id":"2","product":"Hourly Rate","description":"Business Rate: Consulting/Labor/Installs","cost":"150.00","quantity":"2","price":"300.00","timestamp":"2019-02-05 02:22:47"},{"autonum":"43","date":"2018-11-12","customer":"jad Rasul","address":"Washington, DC 20017\r\n\t\t\t","invoice":"20181024","sales":"893.00","paid":"300.00","owed":"593.00","tax":"0.00","misc":"2018-11-12 18:32:13","cust_id":"27","product":"Win10-key","description":"OEM Windows 10 Home, 64-Bit, 1-Pack, DVD","cost":"98.99","quantity":"1","price":"98.99","timestamp":"2018-11-12 18:32:13"},{"autonum":"89","date":"2019-02-25","customer":"jad Rasul","address":"Washington, DC 20017\r\nSuite 114","invoice":"20190225","sales":"375.00","paid":"0.00","owed":"375.00","tax":"0.00","misc":"2019-02-26 02:22:47","cust_id":"56","product":"Hourly Rate","description":"Commercial Rate - troubleshoot and repair network after Comc","cost":"150.00","quantity":"3","price":"375.00","timestamp":"2019-02-26 02:22:47"},{"autonum":"33","date":"2018-09-02","customer":"Potomac\r\n","address":"Damascus, MD 20872\t\t\t","invoice":"20180902","sales":"467.00","paid":"0.00","owed":"467.00","tax":"0.00","misc":"2018-09-02 22:44:09","cust_id":"23","product":"Hourly Rate","description":"Commercial Camera Installation / Mount / Seal","cost":"150.00","quantity":"2","price":"300.00","timestamp":"2018-09-02 22:44:09"},{"autonum":"30","date":"2017-08-14","customer":"Ann Frank\r\n","address":"Ellicott City, MD 21043\r\n\r\n\t\t\t","invoice":"20170814","sales":"350.00","paid":"0.00","owed":"350.00","tax":"0.00","misc":"2017-08-14 17:56:52","cust_id":"21","product":"Hourly Rate","description":"Residential Rate: Labor/Installs: 6 Kidde i12010S 10 year sm","cost":"350.00","quantity":"1","price":"350.00","timestamp":"2017-08-14 17:56:52"},{"autonum":"21","date":"2017-03-22","customer":"Bikes Music\r\n","address":"Mery Station","invoice":"20160929","sales":"2416.00","paid":"1000.00","owed":"1416.00","tax":"0.00","misc":"2017-03-22 19:51:00","cust_id":"15","product":"Home Depot","description":"Home Depot Supplies for Shelf/mounts/braces/paint.","cost":"128.50","quantity":"1","price":"128.50","timestamp":"2017-03-22 19:51:00"},{"autonum":"83","date":"2019-02-07","customer":"Bikes Music","address":"Mery Station","invoice":"20190207","sales":"95.00","paid":"0.00","owed":"95.00","tax":"0.00","misc":"2019-02-15 20:57:54","cust_id":"50","product":"Hourly Rate","description":"Residential Rate: Consulting/Labor/Installs","cost":"95.00","quantity":"1","price":"95.00","timestamp":"2019-02-15 20:57:54"}]
json.sort(( a, b ) =>
a.customer.localeCompare(b.customer) || +a.invoice - +b.invoice
);
console.log(json);