{"id":8022,"date":"2015-11-21T13:41:08","date_gmt":"2015-11-21T13:41:08","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2015\/11\/21\/how-can-i-use-lodash-underscore-to-sort-by-multiple-nested-fields-open-source-projects-lodash-lodash\/"},"modified":"2022-08-30T15:03:03","modified_gmt":"2022-08-30T15:03:03","slug":"how-can-i-use-lodash-underscore-to-sort-by-multiple-nested-fields-open-source-projects-lodash-lodash","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2015\/11\/21\/how-can-i-use-lodash-underscore-to-sort-by-multiple-nested-fields-open-source-projects-lodash-lodash\/","title":{"rendered":"How can I use lodash\/underscore to sort by multiple nested fields?-open source projects lodash\/lodash"},"content":{"rendered":"<p><img decoding=\"async\" src=\"http:\/\/www.gravatar.com\/avatar\/4d8c56a5666c27f4fe7afb977087a86b?s=128&amp;d=identicon&amp;r=PG\" \/> <strong>Tomas Kirda<\/strong><\/p>\n<p>There is a _.sortByAll method in lodash version 3:<\/p>\n<p>https:\/\/lodash.com\/docs#sortByAll<\/p>\n<p>Other option would be to sort values yourself:<\/p>\n<p>https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/sort<\/p>\n<pre><code>function compareValues(v1, v2) {\n    return (v1 &gt; v2) \n        ? 1 \n        : (v1 &lt; v2 ? -1 : 0);\n};\n\n\nvar data = [\n    { a: 2, b: 1 },\n    { a: 2, b: 2 },\n    { a: 1, b: 3 }\n];\n\ndata.sort(function (x, y) {\n    var result = compareValues(x.a, y.a);\n\n    return result === 0 \n        ? compareValues(x.b, y.b) \n        : result;\n});\n\n\/\/ data after sort:\n\/\/ [\n\/\/     { a: 1, b: 3 },\n\/\/     { a: 2, b: 1 },\n\/\/     { a: 2, b: 2 }\n\/\/ ];\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Tomas Kirda There is a _.sortByAll method in lodash version 3: https:\/\/lodash.com\/docs#sortByAll Other option would be to sort values yourself: https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/sort function compareValues(v1, v2) { return (v1 &gt; v2) ? 1 : (v1 &lt; v2 ? -1 : 0); }; var data = [ { a: 2, b: 1 }, { a: 2, b: 2 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-8022","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8022","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=8022"}],"version-history":[{"count":1,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8022\/revisions"}],"predecessor-version":[{"id":8688,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/8022\/revisions\/8688"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=8022"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=8022"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=8022"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}