Best way to sort results of JSON array? I need to re-order by customer
then invoice
keeping all customers together with ascending invoice
values. Same as a SQL would be: SELECT * FROM Table ORDER BY customer, invoice ASC
. Here is attempt: 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"
}
];