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?










1















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"

];












share|improve this question
























  • 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















1















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"

];












share|improve this question
























  • 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













1












1








1








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"

];












share|improve this question
















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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

















  • 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












1 Answer
1






active

oldest

votes


















2














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);








share|improve this answer























  • 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












  • 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












  • got it. and thanks so much, that explains it..

    – BarclayVision
    Mar 7 at 15:53











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
);



);













draft saved

draft discarded


















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









2














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);








share|improve this answer























  • 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












  • 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












  • got it. and thanks so much, that explains it..

    – BarclayVision
    Mar 7 at 15:53















2














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);








share|improve this answer























  • 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












  • 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












  • got it. and thanks so much, that explains it..

    – BarclayVision
    Mar 7 at 15:53













2












2








2







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);








share|improve this answer













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);






share|improve this answer












share|improve this answer



share|improve this answer










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 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











  • 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

















  • 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












  • 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












  • 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



















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Save data to MySQL database using ExtJS and PHP [closed]2019 Community Moderator ElectionHow can I prevent SQL injection in PHP?Which MySQL data type to use for storing boolean valuesPHP: Delete an element from an arrayHow do I connect to a MySQL Database in Python?Should I use the datetime or timestamp data type in MySQL?How to get a list of MySQL user accountsHow Do You Parse and Process HTML/XML in PHP?Reference — What does this symbol mean in PHP?How does PHP 'foreach' actually work?Why shouldn't I use mysql_* functions in PHP?

Compiling GNU Global with universal-ctags support Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern) Data science time! April 2019 and salary with experience The Ask Question Wizard is Live!Tags for Emacs: Relationship between etags, ebrowse, cscope, GNU Global and exuberant ctagsVim and Ctags tips and trickscscope or ctags why choose one over the other?scons and ctagsctags cannot open option file “.ctags”Adding tag scopes in universal-ctagsShould I use Universal-ctags?Universal ctags on WindowsHow do I install GNU Global with universal ctags support using Homebrew?Universal ctags with emacsHow to highlight ctags generated by Universal Ctags in Vim?

Add ONERROR event to image from jsp tldHow to add an image to a JPanel?Saving image from PHP URLHTML img scalingCheck if an image is loaded (no errors) with jQueryHow to force an <img> to take up width, even if the image is not loadedHow do I populate hidden form field with a value set in Spring ControllerStyling Raw elements Generated from JSP tagds with Jquery MobileLimit resizing of images with explicitly set width and height attributeserror TLD use in a jsp fileJsp tld files cannot be resolved