sort JSON array by string and number properties in Javascript The Next CEO of Stack OverflowJavascript sort array by two fieldsSort by two values prioritizing on one of themHow do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?How to insert an item into an array at a specific index (JavaScript)?Sort array of objects by string property valueHow to replace all occurrences of a string in JavaScriptHow do I empty an array in JavaScript?How to check whether a string contains a substring in JavaScript?Loop through an array in JavaScriptHow do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?
Make solar eclipses exceedingly rare, but still have new moons
Non-deterministic sum of floats
Are there any limitations on attacking while grappling?
Can we say or write : "No, it'sn't"?
Is there a difference between "Fahrstuhl" and "Aufzug"
Received an invoice from my ex-employer billing me for training; how to handle?
What happened in Rome, when the western empire "fell"?
Would a completely good Muggle be able to use a wand?
sp_blitzCache results Memory grants
Is "for causing autism in X" grammatical?
Why is the US ranked as #45 in Press Freedom ratings, despite its extremely permissive free speech laws?
If a black hole is created from light, can this black hole then move at speed of light?
Written every which way
Bold, vivid family
What is the result of assigning to std::vector<T>::begin()?
Why do professional authors make "consistency" mistakes? And how to avoid them?
Why do we use the plural of movies in this phrase "We went to the movies last night."?
Inappropriate reference requests from Journal reviewers
How to solve a differential equation with a term to a power?
Rotate a column
What exact does MIB represent in SNMP? How is it different from OID?
Would this house-rule that treats advantage as a +1 to the roll instead (and disadvantage as -1) and allows them to stack be balanced?
If Nick Fury and Coulson already knew about aliens (Kree and Skrull) why did they wait until Thor's appearance to start making weapons?
How do I avoid eval and parse?
sort JSON array by string and number properties in Javascript
The Next CEO of Stack OverflowJavascript sort array by two fieldsSort by two values prioritizing on one of themHow do I remove a property from a JavaScript object?How do I check if an array includes an object in JavaScript?How to insert an item into an array at a specific index (JavaScript)?Sort array of objects by string property valueHow to replace all occurrences of a string in JavaScriptHow do I empty an array in JavaScript?How to check whether a string contains a substring in JavaScript?Loop through an array in JavaScriptHow do I remove a particular element from an array in JavaScript?For-each over an array in JavaScript?
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 Rasulrn",
"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 Rasulrn",
"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 Rasulrn",
"address": "1160 Varnum StreetrnSuite 114rnWashington, DC 20017rnttt",
"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 20017rnttt",
"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 20017rnSuite 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": "Potomacrn",
"address": "Damascus, MD 20872ttt",
"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 Frankrn",
"address": "Ellicott City, MD 21043rnrnttt",
"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 Musicrn",
"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"
];
javascript arrays json sorting
add a comment |
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 Rasulrn",
"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 Rasulrn",
"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 Rasulrn",
"address": "1160 Varnum StreetrnSuite 114rnWashington, DC 20017rnttt",
"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 20017rnttt",
"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 20017rnSuite 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": "Potomacrn",
"address": "Damascus, MD 20872ttt",
"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 Frankrn",
"address": "Ellicott City, MD 21043rnrnttt",
"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 Musicrn",
"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"
];
javascript arrays json sorting
Possible duplicate of Sort by two values prioritizing on one of them
– msanford
Mar 7 at 15:09
FYI it's only JSON if it is a source string (that can be be parsed). What you have is a javascript array containing javascript objects.
– Peter B
Mar 7 at 15:09
2
Possible duplicate of Javascript sort array by two fields
– Peter B
Mar 7 at 15:13
I would just use lodash -_.sortBy(values, ['customer', 'invoice'])
. You can import only the sortBy function depending on your setup.
– Dominic
Mar 7 at 15:15
add a comment |
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 Rasulrn",
"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 Rasulrn",
"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 Rasulrn",
"address": "1160 Varnum StreetrnSuite 114rnWashington, DC 20017rnttt",
"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 20017rnttt",
"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 20017rnSuite 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": "Potomacrn",
"address": "Damascus, MD 20872ttt",
"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 Frankrn",
"address": "Ellicott City, MD 21043rnrnttt",
"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 Musicrn",
"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"
];
javascript arrays json sorting
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 Rasulrn",
"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 Rasulrn",
"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 Rasulrn",
"address": "1160 Varnum StreetrnSuite 114rnWashington, DC 20017rnttt",
"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 20017rnttt",
"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 20017rnSuite 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": "Potomacrn",
"address": "Damascus, MD 20872ttt",
"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 Frankrn",
"address": "Ellicott City, MD 21043rnrnttt",
"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 Musicrn",
"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"
];
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 Rasulrn",
"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 Rasulrn",
"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 Rasulrn",
"address": "1160 Varnum StreetrnSuite 114rnWashington, DC 20017rnttt",
"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 20017rnttt",
"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 20017rnSuite 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": "Potomacrn",
"address": "Damascus, MD 20872ttt",
"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 Frankrn",
"address": "Ellicott City, MD 21043rnrnttt",
"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 Musicrn",
"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"
];
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 Rasulrn",
"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 Rasulrn",
"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 Rasulrn",
"address": "1160 Varnum StreetrnSuite 114rnWashington, DC 20017rnttt",
"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 20017rnttt",
"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 20017rnSuite 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": "Potomacrn",
"address": "Damascus, MD 20872ttt",
"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 Frankrn",
"address": "Ellicott City, MD 21043rnrnttt",
"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 Musicrn",
"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"
];
javascript arrays json sorting
javascript arrays json sorting
edited Mar 8 at 8:04
adiga
12.1k62545
12.1k62545
asked Mar 7 at 15:02
BarclayVisionBarclayVision
5051632
5051632
Possible duplicate of Sort by two values prioritizing on one of them
– msanford
Mar 7 at 15:09
FYI it's only JSON if it is a source string (that can be be parsed). What you have is a javascript array containing javascript objects.
– Peter B
Mar 7 at 15:09
2
Possible duplicate of Javascript sort array by two fields
– Peter B
Mar 7 at 15:13
I would just use lodash -_.sortBy(values, ['customer', 'invoice'])
. You can import only the sortBy function depending on your setup.
– Dominic
Mar 7 at 15:15
add a comment |
Possible duplicate of Sort by two values prioritizing on one of them
– msanford
Mar 7 at 15:09
FYI it's only JSON if it is a source string (that can be be parsed). What you have is a javascript array containing javascript objects.
– Peter B
Mar 7 at 15:09
2
Possible duplicate of Javascript sort array by two fields
– Peter B
Mar 7 at 15:13
I would just use lodash -_.sortBy(values, ['customer', 'invoice'])
. You can import only the sortBy function depending on your setup.
– Dominic
Mar 7 at 15:15
Possible duplicate of Sort by two values prioritizing on one of them
– msanford
Mar 7 at 15:09
Possible duplicate of Sort by two values prioritizing on one of them
– msanford
Mar 7 at 15:09
FYI it's only JSON if it is a source string (that can be be parsed). What you have is a javascript array containing javascript objects.
– Peter B
Mar 7 at 15:09
FYI it's only JSON if it is a source string (that can be be parsed). What you have is a javascript array containing javascript objects.
– Peter B
Mar 7 at 15:09
2
2
Possible duplicate of Javascript sort array by two fields
– Peter B
Mar 7 at 15:13
Possible duplicate of Javascript sort array by two fields
– Peter B
Mar 7 at 15:13
I would just use lodash -
_.sortBy(values, ['customer', 'invoice'])
. You can import only the sortBy function depending on your setup.– Dominic
Mar 7 at 15:15
I would just use lodash -
_.sortBy(values, ['customer', 'invoice'])
. You can import only the sortBy function depending on your setup.– Dominic
Mar 7 at 15:15
add a comment |
1 Answer
1
active
oldest
votes
You can use sort
by comparing the customer
values first using localeCompare
. If both are same, then compare the numeric values of invoice
var json=["autonum":"26","date":"2017-04-17","customer":"jad Rasulrn","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 Rasulrn","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 Rasulrn","address":"1160 Varnum StreetrnSuite 114rnWashington, DC 20017rnttt","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 20017rnttt","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 20017rnSuite 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":"Potomacrn","address":"Damascus, MD 20872ttt","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 Frankrn","address":"Ellicott City, MD 21043rnrnttt","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 Musicrn","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);
invoice are not asc - the jump from 2019 2017 2017 2018
– BarclayVision
Mar 7 at 15:30
First it sorts bycustomer
. If 2 items have samecustomer
THEN within the customer, it sorts byinvoice
. Check the values forjad Rasul
– adiga
Mar 7 at 15:32
look atjad
order of invoice is 20181022, 20190225, 20170412, 20170731, 20180611
– BarclayVision
Mar 7 at 15:38
Because they are not the samecustomer
. One of them belongs tojad Rasul
and the other isjad Rasulrn
. Firstjad Rasul
values are grouped and sorted based on invoice. Thenjad Rasulrn
values are sorted based oninvoice
. If you want them to be considered as same values, then fix your json by removing thern
values.
– adiga
Mar 7 at 15:50
got it. and thanks so much, that explains it..
– BarclayVision
Mar 7 at 15:53
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55046874%2fsort-json-array-by-string-and-number-properties-in-javascript%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
You can use sort
by comparing the customer
values first using localeCompare
. If both are same, then compare the numeric values of invoice
var json=["autonum":"26","date":"2017-04-17","customer":"jad Rasulrn","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 Rasulrn","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 Rasulrn","address":"1160 Varnum StreetrnSuite 114rnWashington, DC 20017rnttt","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 20017rnttt","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 20017rnSuite 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":"Potomacrn","address":"Damascus, MD 20872ttt","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 Frankrn","address":"Ellicott City, MD 21043rnrnttt","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 Musicrn","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);
invoice are not asc - the jump from 2019 2017 2017 2018
– BarclayVision
Mar 7 at 15:30
First it sorts bycustomer
. If 2 items have samecustomer
THEN within the customer, it sorts byinvoice
. Check the values forjad Rasul
– adiga
Mar 7 at 15:32
look atjad
order of invoice is 20181022, 20190225, 20170412, 20170731, 20180611
– BarclayVision
Mar 7 at 15:38
Because they are not the samecustomer
. One of them belongs tojad Rasul
and the other isjad Rasulrn
. Firstjad Rasul
values are grouped and sorted based on invoice. Thenjad Rasulrn
values are sorted based oninvoice
. If you want them to be considered as same values, then fix your json by removing thern
values.
– adiga
Mar 7 at 15:50
got it. and thanks so much, that explains it..
– BarclayVision
Mar 7 at 15:53
add a comment |
You can use sort
by comparing the customer
values first using localeCompare
. If both are same, then compare the numeric values of invoice
var json=["autonum":"26","date":"2017-04-17","customer":"jad Rasulrn","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 Rasulrn","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 Rasulrn","address":"1160 Varnum StreetrnSuite 114rnWashington, DC 20017rnttt","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 20017rnttt","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 20017rnSuite 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":"Potomacrn","address":"Damascus, MD 20872ttt","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 Frankrn","address":"Ellicott City, MD 21043rnrnttt","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 Musicrn","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);
invoice are not asc - the jump from 2019 2017 2017 2018
– BarclayVision
Mar 7 at 15:30
First it sorts bycustomer
. If 2 items have samecustomer
THEN within the customer, it sorts byinvoice
. Check the values forjad Rasul
– adiga
Mar 7 at 15:32
look atjad
order of invoice is 20181022, 20190225, 20170412, 20170731, 20180611
– BarclayVision
Mar 7 at 15:38
Because they are not the samecustomer
. One of them belongs tojad Rasul
and the other isjad Rasulrn
. Firstjad Rasul
values are grouped and sorted based on invoice. Thenjad Rasulrn
values are sorted based oninvoice
. If you want them to be considered as same values, then fix your json by removing thern
values.
– adiga
Mar 7 at 15:50
got it. and thanks so much, that explains it..
– BarclayVision
Mar 7 at 15:53
add a comment |
You can use sort
by comparing the customer
values first using localeCompare
. If both are same, then compare the numeric values of invoice
var json=["autonum":"26","date":"2017-04-17","customer":"jad Rasulrn","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 Rasulrn","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 Rasulrn","address":"1160 Varnum StreetrnSuite 114rnWashington, DC 20017rnttt","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 20017rnttt","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 20017rnSuite 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":"Potomacrn","address":"Damascus, MD 20872ttt","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 Frankrn","address":"Ellicott City, MD 21043rnrnttt","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 Musicrn","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);
You can use sort
by comparing the customer
values first using localeCompare
. If both are same, then compare the numeric values of invoice
var json=["autonum":"26","date":"2017-04-17","customer":"jad Rasulrn","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 Rasulrn","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 Rasulrn","address":"1160 Varnum StreetrnSuite 114rnWashington, DC 20017rnttt","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 20017rnttt","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 20017rnSuite 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":"Potomacrn","address":"Damascus, MD 20872ttt","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 Frankrn","address":"Ellicott City, MD 21043rnrnttt","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 Musicrn","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);
var json=["autonum":"26","date":"2017-04-17","customer":"jad Rasulrn","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 Rasulrn","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 Rasulrn","address":"1160 Varnum StreetrnSuite 114rnWashington, DC 20017rnttt","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 20017rnttt","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 20017rnSuite 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":"Potomacrn","address":"Damascus, MD 20872ttt","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 Frankrn","address":"Ellicott City, MD 21043rnrnttt","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 Musicrn","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);
var json=["autonum":"26","date":"2017-04-17","customer":"jad Rasulrn","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 Rasulrn","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 Rasulrn","address":"1160 Varnum StreetrnSuite 114rnWashington, DC 20017rnttt","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 20017rnttt","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 20017rnSuite 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":"Potomacrn","address":"Damascus, MD 20872ttt","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 Frankrn","address":"Ellicott City, MD 21043rnrnttt","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 Musicrn","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);
answered Mar 7 at 15:22
adigaadiga
12.1k62545
12.1k62545
invoice are not asc - the jump from 2019 2017 2017 2018
– BarclayVision
Mar 7 at 15:30
First it sorts bycustomer
. If 2 items have samecustomer
THEN within the customer, it sorts byinvoice
. Check the values forjad Rasul
– adiga
Mar 7 at 15:32
look atjad
order of invoice is 20181022, 20190225, 20170412, 20170731, 20180611
– BarclayVision
Mar 7 at 15:38
Because they are not the samecustomer
. One of them belongs tojad Rasul
and the other isjad Rasulrn
. Firstjad Rasul
values are grouped and sorted based on invoice. Thenjad Rasulrn
values are sorted based oninvoice
. If you want them to be considered as same values, then fix your json by removing thern
values.
– adiga
Mar 7 at 15:50
got it. and thanks so much, that explains it..
– BarclayVision
Mar 7 at 15:53
add a comment |
invoice are not asc - the jump from 2019 2017 2017 2018
– BarclayVision
Mar 7 at 15:30
First it sorts bycustomer
. If 2 items have samecustomer
THEN within the customer, it sorts byinvoice
. Check the values forjad Rasul
– adiga
Mar 7 at 15:32
look atjad
order of invoice is 20181022, 20190225, 20170412, 20170731, 20180611
– BarclayVision
Mar 7 at 15:38
Because they are not the samecustomer
. One of them belongs tojad Rasul
and the other isjad Rasulrn
. Firstjad Rasul
values are grouped and sorted based on invoice. Thenjad Rasulrn
values are sorted based oninvoice
. If you want them to be considered as same values, then fix your json by removing thern
values.
– adiga
Mar 7 at 15:50
got it. and thanks so much, that explains it..
– BarclayVision
Mar 7 at 15:53
invoice are not asc - the jump from 2019 2017 2017 2018
– BarclayVision
Mar 7 at 15:30
invoice are not asc - the jump from 2019 2017 2017 2018
– BarclayVision
Mar 7 at 15:30
First it sorts by
customer
. If 2 items have same customer
THEN within the customer, it sorts by invoice
. Check the values for jad Rasul
– adiga
Mar 7 at 15:32
First it sorts by
customer
. If 2 items have same customer
THEN within the customer, it sorts by invoice
. Check the values for jad Rasul
– adiga
Mar 7 at 15:32
look at
jad
order of invoice is 20181022, 20190225, 20170412, 20170731, 20180611– BarclayVision
Mar 7 at 15:38
look at
jad
order of invoice is 20181022, 20190225, 20170412, 20170731, 20180611– BarclayVision
Mar 7 at 15:38
Because they are not the same
customer
. One of them belongs to jad Rasul
and the other is jad Rasulrn
. First jad Rasul
values are grouped and sorted based on invoice. Then jad Rasulrn
values are sorted based on invoice
. If you want them to be considered as same values, then fix your json by removing the rn
values.– adiga
Mar 7 at 15:50
Because they are not the same
customer
. One of them belongs to jad Rasul
and the other is jad Rasulrn
. First jad Rasul
values are grouped and sorted based on invoice. Then jad Rasulrn
values are sorted based on invoice
. If you want them to be considered as same values, then fix your json by removing the rn
values.– adiga
Mar 7 at 15:50
got it. and thanks so much, that explains it..
– BarclayVision
Mar 7 at 15:53
got it. and thanks so much, that explains it..
– BarclayVision
Mar 7 at 15:53
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55046874%2fsort-json-array-by-string-and-number-properties-in-javascript%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Possible duplicate of Sort by two values prioritizing on one of them
– msanford
Mar 7 at 15:09
FYI it's only JSON if it is a source string (that can be be parsed). What you have is a javascript array containing javascript objects.
– Peter B
Mar 7 at 15:09
2
Possible duplicate of Javascript sort array by two fields
– Peter B
Mar 7 at 15:13
I would just use lodash -
_.sortBy(values, ['customer', 'invoice'])
. You can import only the sortBy function depending on your setup.– Dominic
Mar 7 at 15:15