{"id":18960,"date":"2022-09-23T07:36:07","date_gmt":"2022-09-23T14:36:07","guid":{"rendered":"https:\/\/coderpad.io\/?p=18960"},"modified":"2023-06-05T14:04:32","modified_gmt":"2023-06-05T21:04:32","slug":"guide-to-sorting-in-javascript","status":"publish","type":"post","link":"https:\/\/coderpad.io\/blog\/development\/guide-to-sorting-in-javascript\/","title":{"rendered":"Guide to Sorting in JavaScript"},"content":{"rendered":"\n<p>Like sorting your laundry? Or cleaning up a Lego spill into neat color-coded piles?<\/p>\n\n\n\n<p>Yeah, me neither. <\/p>\n\n\n\n<p>Because of its repetitive nature, sorting can be a bit of a chore at times. Unfortunately, sorting is as necessary for programming as it is in our daily lives. <\/p>\n\n\n\n<p>Fortunately for us, however, it\u2019s much easier to do as a JavaScript developer than it is in other areas of our lives.<\/p>\n\n\n\n<p>In this guide, we&#8217;ll look at:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>How to use JavaScript&#8217;s built in <code>sort<\/code> method<\/li><li>How to sort integer and string arrays<\/li><li>How to sort object arrays<\/li><li>How to sort non-ASCII characters<\/li><li>What Lodash is<ul><li>How to use Lodash&#8217;s <code>sortBy<\/code> method<\/li><li>How to use Lodash&#8217;s <code>orderBy<\/code> method<\/li><\/ul><\/li><li>A comparison of performance metrics for different sorting mechanisms<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Organize your data using the JavaScript sort method<\/h2>\n\n\n\n<p>Sorting is the process of organizing data into meaningful groups so that it can be easily analyzed or processed. <\/p>\n\n\n\n<p>JavaScript, like most other programming languages, includes a built-in sort method with the following syntax:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-tag\">array<\/span><span class=\"hljs-selector-class\">.sort<\/span>(<span class=\"hljs-selector-tag\">compareFunction<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Where the <code>array<\/code> is an array identifier containing the elements we want to sort, and the <code>compareFunction<\/code> is an optional function to enhance our sorting logic further.&nbsp;<\/p>\n\n\n\n<p>If a compare function is not supplied, the array elements are sorted by first converting them to strings, which are then compared and sorted in ascending order.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How the sort function works<\/h3>\n\n\n\n<p>The <code>.sort()<\/code> method uses an algorithm called &#8220;in-place&#8221; to sort the elements in our array. This type of algorithm does not require extra disk space to sort the items but may require a small, non-constant amount of extra memory space to run. Once the sorting operation is complete, it will return the same array, now sorted, without creating a new array in the process.<\/p>\n\n\n\n<p>Here\u2019s an example of utilizing <code>sort()<\/code> on an array of strings:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">let<\/span> states = &#91;<span class=\"hljs-string\">\"Lagos\"<\/span>, <span class=\"hljs-string\">\"New York\"<\/span>, <span class=\"hljs-string\">\"Toronto\"<\/span>, <span class=\"hljs-string\">\"Beijing\"<\/span>, <span class=\"hljs-string\">\"Amsterdam\"<\/span>];\nstates.sort();\n<span class=\"hljs-built_in\">console<\/span>.log(states);\n\n<span class=\"hljs-comment\">\/\/ returns: &#91;\"Amsterdam\", \"Beijing\", \"Lagos\", \"New York\", \"Toronto\"]<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here, we can see that our strings are sorted using the <a href=\"https:\/\/en.wikipedia.org\/wiki\/Latin_alphabet\" target=\"_blank\" rel=\"noreferrer noopener\">latin alphabet&#8217;s<\/a> letter order. This means that &#8220;Aardvark&#8221; will come before &#8220;Zigzag&#8221;.<\/p>\n\n\n\n<p>You can also apply <code>.sort()<\/code> to simple integers:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">let<\/span> luckyNumbers = &#91;<span class=\"hljs-number\">6<\/span>, <span class=\"hljs-number\">1<\/span>, <span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">7<\/span>, <span class=\"hljs-number\">2<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">3<\/span>];\nluckyNumbers.sort();\n<span class=\"hljs-built_in\">console<\/span>.log(luckyNumbers);\n\n<span class=\"hljs-comment\">\/\/ returns: &#91;1, 2, 3, 4, 5, 6, 7]<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>However, when we try to sort through integers with larger values, our sort function appears to fail:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">let<\/span> luckyNumbers = &#91;<span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">40<\/span>, <span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">300<\/span>, <span class=\"hljs-number\">20<\/span>, <span class=\"hljs-number\">50<\/span>, <span class=\"hljs-number\">30<\/span>];\nluckyNumbers.sort();\n<span class=\"hljs-built_in\">console<\/span>.log(luckyNumbers);\n\n<span class=\"hljs-comment\">\/\/ returns: &#91;10, 100, 20, 30, 300, 40, 50]<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>So, what is causing this? As previously stated, <strong>if a compare function is not provided in the <code>.sort()<\/code> operation, all array elements are sorted by first converting them to strings and then comparing them.&nbsp;<\/strong><\/p>\n\n\n\n<p>In other words, while our <code>.sort()<\/code>&nbsp; method continues to return an array of integers, we are actually attempting to sort the following array:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"JSON \/ JSON with Comments\" data-shcb-language-slug=\"json\"><span><code class=\"hljs language-json shcb-wrap-lines\">&#91;<span class=\"hljs-string\">\"100\"<\/span>, <span class=\"hljs-string\">\"40\"<\/span>, <span class=\"hljs-string\">\"10\"<\/span>, <span class=\"hljs-string\">\"300\"<\/span>, <span class=\"hljs-string\">\"20\"<\/span>, <span class=\"hljs-string\">\"50\"<\/span>, <span class=\"hljs-string\">\"30\"<\/span>]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JSON \/ JSON with Comments<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">json<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And alphabetically, &#8220;100&#8221; comes before &#8220;20&#8221;. We can quickly fix this by writing our own custom compare function instead of using JavaScript&#8217;s default.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Use custom logic to sort your data by creating a compare function<\/h3>\n\n\n\n<p>The compare function takes two parameters, commonly named <code>a<\/code> and <code>b<\/code>, where <code>a<\/code> is the first element for comparison and <code>b<\/code> is the second. Because <code>b<\/code> is the second value, it is commonly referred to as the comparator. This function then runs during the <code>.sort()<\/code> method and assists it in determining which item in the array should come first in the results.<\/p>\n\n\n\n<p>Furthermore, the compare function must be written in such a way that:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>It returns a negative number if the first comparator should be sorted before the second.<\/li><li>It returns 0 (zero) if the comparators should be equal or maintain the same order.<\/li><li>It returns a positive number if the second comparator should be sorted before the first.<\/li><\/ul>\n\n\n\n<figure class=\"wp-block-table\"><table><tbody><tr><td><strong>`compareFn(a, b)` return value<\/strong><\/td><td><strong>sort order<\/strong><\/td><\/tr><tr><td>&gt; 0<\/td><td>sort `a` after `b`<\/td><\/tr><tr><td>&lt; 0<\/td><td>sort `a` before `b`<\/td><\/tr><tr><td>=== 0<\/td><td>keep original order of `a` and `b`<\/td><\/tr><\/tbody><\/table><figcaption>Table Credit: <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/Array\/sort#description\" target=\"_blank\" rel=\"noreferrer noopener\">MDN<\/a><\/figcaption><\/figure>\n\n\n\n<p>To better understand this, imagine we have a three-element array of numbers:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">let<\/span> numList = &#91;<span class=\"hljs-number\">4<\/span>, <span class=\"hljs-number\">5<\/span>, <span class=\"hljs-number\">3<\/span>];<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And, we&#8217;d like to sort it in descending<strong> <\/strong>order. We could write a compare function and apply it to our array in the following way:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> <span class=\"hljs-title\">compare<\/span>(<span class=\"hljs-params\">a, b<\/span>) <\/span>{\n\u00a0 <span class=\"hljs-keyword\">return<\/span> b - a;\n}\nnumList.sort(compare);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Behind the scenes, <code>.sort()<\/code> will call the <code>compare<\/code> function repeatedly as it runs through the entire array, picking two elements at a time and using the logic in our compare function to determine which item comes before which. For example, let\u2019s look at what happens when we sort the first two numbers in the array:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs shcb-wrap-lines\">compare(4, 5);<\/code><\/span><\/pre>\n\n\n<p>According to our compare function, the code above will return <code>1<\/code> (i.e., 5 &#8211; 4), which is a positive number. Looking at our comparison table, we must return a positive number if we want the second comparator (or, value) to be sorted before the first, which translates to 5 coming before 4, so we now have [5, 4] stored in memory; it will keep doing this process for each element in the array until the desired result is obtained.<\/p>\n\n\n\n<p>Additionally, while the preceding example used a named compare function, we&#8217;re also able to&nbsp; use inline functions instead.<\/p>\n\n\n\n<p>Here&#8217;s a simplified implementation example which sorts our previous large integer array in ascending order:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">let<\/span> luckyNumbers = &#91;<span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">40<\/span>, <span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">300<\/span>, <span class=\"hljs-number\">20<\/span>, <span class=\"hljs-number\">50<\/span>, <span class=\"hljs-number\">30<\/span>];\nluckyNumbers.sort(<span class=\"hljs-function\"><span class=\"hljs-keyword\">function<\/span> (<span class=\"hljs-params\">a, b<\/span>) <\/span>{\n  <span class=\"hljs-keyword\">return<\/span> a - b;\n});\n<span class=\"hljs-built_in\">console<\/span>.log(luckyNumbers);\n\n<span class=\"hljs-comment\">\/\/ returns: &#91;10, 20, 30, 40, 50, 100, 300]<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And here\u2019s an example of sorting the same array in descending order, but simplified even further by using the <a href=\"https:\/\/www.w3schools.com\/js\/js_arrow_function.asp\" target=\"_blank\" rel=\"noreferrer noopener\">ES6 arrow function<\/a>:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">let<\/span> luckyNumbers = &#91;<span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">40<\/span>, <span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">300<\/span>, <span class=\"hljs-number\">20<\/span>, <span class=\"hljs-number\">50<\/span>, <span class=\"hljs-number\">30<\/span>];\nluckyNumbers.sort(<span class=\"hljs-function\">(<span class=\"hljs-params\">a, b<\/span>) =&gt;<\/span> b - a);\n<span class=\"hljs-built_in\">console<\/span>.log(luckyNumbers);\n\n<span class=\"hljs-comment\">\/\/ returns: &#91;300, 100, 50, 40, 30, 20, 10]<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>While using math in our compare function (eg <code>b-a<\/code>) to determine our comparison value works well with integers, it&#8217;s often not viable when working with other data types, such as strings and booleans. Instead, we can include logic (such as adding <code>if\/else<\/code> statements) in our compare function. We can then return -1, 1, or 0 based on this logic.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to sort different data types<\/h2>\n\n\n\n<p>Now that we fully understand how the compare function works, let&#8217;s look at some examples to see where it might be helpful.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">How to sort an array of integers and strings<\/h3>\n\n\n\n<p>Let&#8217;s say that we have a list of strings and integers, like so:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-attr\">&#91;123, <span class=\"hljs-string\">\"John\"<\/span>, <span class=\"hljs-string\">\"Zuck\"<\/span>, 345, <span class=\"hljs-string\">\"Ben\"<\/span>, <span class=\"hljs-string\">\"Alan\"<\/span>, 678, <span class=\"hljs-string\">\"Mom\"<\/span>]<\/span>;<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And we want to sort them such that:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>Numbers come first, sorted numerically<\/li><li>Strings come second, sorted alphabetically<\/li><\/ul>\n\n\n\n<p>We could write some custom comparison code, like so, to achieve this effect:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">let<\/span> contacts = &#91;<span class=\"hljs-number\">123<\/span>, <span class=\"hljs-string\">\"John\"<\/span>, <span class=\"hljs-string\">\"Zuck\"<\/span>, <span class=\"hljs-number\">345<\/span>, <span class=\"hljs-string\">\"Ben\"<\/span>, <span class=\"hljs-string\">\"Alan\"<\/span>, <span class=\"hljs-number\">678<\/span>, <span class=\"hljs-string\">\"Mom\"<\/span>];\n\ncontacts.sort(<span class=\"hljs-function\">(<span class=\"hljs-params\">a, b<\/span>) =&gt;<\/span> {\n\u00a0 <span class=\"hljs-keyword\">if<\/span> (<span class=\"hljs-keyword\">typeof<\/span> a === <span class=\"hljs-string\">\"number\"<\/span> &amp;&amp; <span class=\"hljs-keyword\">typeof<\/span> b === <span class=\"hljs-string\">\"number\"<\/span>) {\n\u00a0 \u00a0 <span class=\"hljs-keyword\">return<\/span> a - b;\n\u00a0 }\n\u00a0 <span class=\"hljs-keyword\">if<\/span> (<span class=\"hljs-keyword\">typeof<\/span> a === <span class=\"hljs-string\">\"number\"<\/span>) {\n\u00a0 \u00a0 <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-number\">-1<\/span>;\n\u00a0 }\n\u00a0 <span class=\"hljs-keyword\">if<\/span> (<span class=\"hljs-keyword\">typeof<\/span> b === <span class=\"hljs-string\">\"number\"<\/span>) {\n\u00a0 \u00a0 <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-number\">1<\/span>;\n\u00a0 } <span class=\"hljs-keyword\">else<\/span> {\n\u00a0 \u00a0 <span class=\"hljs-keyword\">return<\/span> a &gt; b ? <span class=\"hljs-number\">1<\/span> : <span class=\"hljs-number\">-1<\/span>;\n\u00a0 }\n});\n\n<span class=\"hljs-built_in\">console<\/span>.log(contacts);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The final output of this function on the array would be:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"JSON \/ JSON with Comments\" data-shcb-language-slug=\"json\"><span><code class=\"hljs language-json shcb-wrap-lines\">&#91;<span class=\"hljs-number\">123<\/span>, <span class=\"hljs-number\">345<\/span>, <span class=\"hljs-number\">678<\/span>, <span class=\"hljs-string\">\"Alan\"<\/span>, <span class=\"hljs-string\">\"Ben\"<\/span>, <span class=\"hljs-string\">\"John\"<\/span>, <span class=\"hljs-string\">\"Mom\"<\/span>, <span class=\"hljs-string\">\"Zuck\"<\/span>]<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JSON \/ JSON with Comments<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">json<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In our compare function, we first define an <code>if<\/code> statement to see if both of our comparators are integers, and then we sort them in ascending order using the previous <code>a - b<\/code> syntax.&nbsp;<\/p>\n\n\n\n<p>We also used two more <code>if<\/code> statements to determine whether the first or only the second comparator is an integer and sorted them accordingly.&nbsp;<\/p>\n\n\n\n<p>Finally, if our comparators are not integers, we used an <code>else<\/code> statement and a ternary operation to sort them by checking which string is greater than the other and returning an integer value for our preferred sort operation.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Sorting an array of objects<\/h3>\n\n\n\n<p>We can also use a compare function to sort an array of objects. Consider the following list of popular JavaScript libraries and their GitHub star count:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">let<\/span> jsLibraries = &#91;\n\u00a0 {\n\u00a0 \u00a0 <span class=\"hljs-attr\">name<\/span>: <span class=\"hljs-string\">\"React\"<\/span>,\n\u00a0 \u00a0 <span class=\"hljs-attr\">star<\/span>: <span class=\"hljs-number\">194478<\/span>,\n\u00a0 },\n\u00a0 {\n\u00a0 \u00a0 <span class=\"hljs-attr\">name<\/span>: <span class=\"hljs-string\">\"Vue\"<\/span>,\n\u00a0 \u00a0 <span class=\"hljs-attr\">star<\/span>: <span class=\"hljs-number\">199199<\/span>,\n\u00a0 },\n\u00a0 {\n\u00a0 \u00a0 <span class=\"hljs-attr\">name<\/span>: <span class=\"hljs-string\">\"Angular\"<\/span>,\n\u00a0 \u00a0 <span class=\"hljs-attr\">star<\/span>: <span class=\"hljs-number\">83716<\/span>,\n\u00a0 },\n\u00a0 {\n\u00a0 \u00a0 <span class=\"hljs-attr\">name<\/span>: <span class=\"hljs-string\">\"Lodash\"<\/span>,\n\u00a0 \u00a0 <span class=\"hljs-attr\">star<\/span>: <span class=\"hljs-number\">54288<\/span>,\n\u00a0 },\n];<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>We can sort it by library name, ascending:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">jsLibraries.sort(<span class=\"hljs-function\">(<span class=\"hljs-params\">a, b<\/span>) =&gt;<\/span> {\n\u00a0 <span class=\"hljs-keyword\">if<\/span> (a.name &gt; b.name) {\n\u00a0 \u00a0 <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-number\">1<\/span>;\n\u00a0 } <span class=\"hljs-keyword\">else<\/span> {\n\u00a0 \u00a0 <span class=\"hljs-keyword\">return<\/span> <span class=\"hljs-number\">-1<\/span>;\n\u00a0 }\n});\n\n<span class=\"hljs-comment\">\/* returns:\n&#91;\n\u00a0 { name: 'Angular', star: 83716 },\n\u00a0 { name: 'Lodash', star: 54288 },\n\u00a0 { name: 'React', star: 194478 },\n\u00a0 { name: 'Vue', star: 199199 }\n]\n*\/<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>And, because the number of stars is an integer, we can use the previous <code>b - a<\/code> syntax to sort in descending order (i.e., libraries with the most stars come first):<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\">jsLibraries.sort(<span class=\"hljs-function\">(<span class=\"hljs-params\">a, b<\/span>) =&gt;<\/span> {\n  <span class=\"hljs-keyword\">return<\/span> b.star - a.star;\n});\n\n<span class=\"hljs-comment\">\/* returns:\n&#91;\n  { name: 'Vue', star: 199199 },\n  { name: 'React', star: 194478 },\n  { name: 'Angular', star: 83716 },\n  { name: 'Lodash', star: 54288 }\n]\n*\/<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Sorting non-ASCII characters<\/h3>\n\n\n\n<p>So far, our sort function has performed admirably with strings. However, when we attempt to sort an array of strings containing special (non-ASCII) characters, the sort method will not work as expected. Consider the following example:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">const<\/span> frenchWords = &#91;<span class=\"hljs-string\">\"r\u00e9sum\u00e9\"<\/span>, <span class=\"hljs-string\">\"premier\"<\/span>, <span class=\"hljs-string\">\"communiqu\u00e9\"<\/span>, <span class=\"hljs-string\">\"caf\u00e9\"<\/span>, <span class=\"hljs-string\">\"adieu\"<\/span>, <span class=\"hljs-string\">\"\u00e9clair\"<\/span>];\nfrenchWords.sort();\n<span class=\"hljs-built_in\">console<\/span>.log(frenchWords);\n\n<span class=\"hljs-comment\">\/\/ returns: &#91;\"adieu\", \"caf\u00e9\", \"communiqu\u00e9\", \"premier\", \"r\u00e9sum\u00e9\", \"\u00e9clair\"]<\/span>\n<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>As seen in this example, the word &#8220;\u00e9clair&#8221; was the last in the array, owing to the fact that it begins with a special character. We can easily fix this by including an additional <code>.localCompare()<\/code> method in our sort compare function.<\/p>\n\n\n\n<p>The <code>.localeCompare()<\/code> method allows us to compare the order of two different strings, i.e., which one comes first or if both strings are in the same order. We can apply this method in a custom compare function to sort non-ASCII characters, such as with languages that include accents or special characters.<\/p>\n\n\n\n<p>This also allows for much flexibility because the <code>.localeCompare()<\/code> method will return a negative number if the reference string comes before the other string we&#8217;re comparing with, a negative number if it occurs after, and 0 if they are equivalent.<\/p>\n\n\n\n<p>To fix our previous example, we can simply apply the <code>.localCompare()<\/code> method like below:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">const<\/span> frenchWords = &#91;<span class=\"hljs-string\">\"r\u00e9sum\u00e9\"<\/span>, <span class=\"hljs-string\">\"premier\"<\/span>, <span class=\"hljs-string\">\"communiqu\u00e9\"<\/span>, <span class=\"hljs-string\">\"caf\u00e9\"<\/span>, <span class=\"hljs-string\">\"adieu\"<\/span>, <span class=\"hljs-string\">\"\u00e9clair\"<\/span>];\nfrenchWords.sort(<span class=\"hljs-function\">(<span class=\"hljs-params\">a, b<\/span>) =&gt;<\/span> a.localeCompare(b, <span class=\"hljs-string\">\"fr\"<\/span>));\n<span class=\"hljs-built_in\">console<\/span>.log(frenchWords);\n\n<span class=\"hljs-comment\">\/\/ returns: &#91;\"adieu\", \"caf\u00e9\", \"communiqu\u00e9\", \"premier\", \"r\u00e9sum\u00e9\"]<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In this example, we applied the <code>.localCompare()<\/code> method to compare the first comparator <code>a<\/code> with the second <code>b<\/code> so that the strings will be sorted in order accordingly.&nbsp;<\/p>\n\n\n\n<p>And, as you might have noticed, there is&nbsp; an optional <code>\u201cfr\u201d<\/code> locale added as the second argument to the <code>.localeCompare()<\/code> method so that our strings will be compared by their order in the French language. In addition to the locale, the <a href=\"https:\/\/developer.mozilla.org\/en-US\/docs\/Web\/JavaScript\/Reference\/Global_Objects\/String\/localeCompare#parameters\" target=\"_blank\" rel=\"noreferrer noopener\">.localeCompare()<\/a> method also accepts additional parameters such as case sensitivity.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Use Lodash for advanced sorting logic<\/h2>\n\n\n\n<p>Lodash is a JavaScript library that provides utility methods for everyday programming tasks such as array, number, object, string, and other data structure manipulation. One of these utility methods helps in sorting arrays. We&#8217;ll see how this works by recreating our previous examples with the <code>lodash<\/code> method.&nbsp;<\/p>\n\n\n\n<p>First, you can install <code>lodash<\/code> via npm with the following command:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs shcb-wrap-lines\">npm i lodash<\/code><\/span><\/pre>\n\n\n<p>And once the installation process is complete, you can import the <code>lodash<\/code> core with:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">const<\/span> _ = <span class=\"hljs-built_in\">require<\/span>(<span class=\"hljs-string\">\"lodash\"<\/span>);<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">Do simple sorting using Lodash&#8217;s _.sortBy()<\/h3>\n\n\n\n<p>The Lodash <code>_.sortBy()<\/code> method is similar to the JavaScript native <code>.sort()<\/code> method in that it takes the array to sort as the first parameter and the sort logic (i.e. the compare function) as the second. Unlike <code>.sort()<\/code>, however, the <code>_.sortBy()<\/code> method does not directly modify the original array and instead creates a new copy during its execution.<\/p>\n\n\n\n<p>Furthermore, the <code>_.sortBy()<\/code> method can only be used to sort arrays in ascending order. However, it is more intuitive than the native <code>.sort()<\/code> method in that you can simply pass a sort order or the key(s) you want to sort with (when working with a collection) and avoid writing any sort logic.<\/p>\n\n\n\n<p>Using our previous example, we can use <code>lodash<\/code> to sort an array of integers or strings as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-19\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">let<\/span> states = &#91;<span class=\"hljs-string\">\"Lagos\"<\/span>, <span class=\"hljs-string\">\"New York\"<\/span>, <span class=\"hljs-string\">\"Toronto\"<\/span>, <span class=\"hljs-string\">\"Beijing\"<\/span>, <span class=\"hljs-string\">\"Amsterdam\"<\/span>];\n<span class=\"hljs-built_in\">console<\/span>.log(_.sortBy(states));\n\n<span class=\"hljs-comment\">\/\/ returns: &#91;\"Amsterdam\", \"Beijing\", \"Lagos\", \"New York\", \"Toronto\"]<\/span>\n\n<span class=\"hljs-comment\">\/* . . . *\/<\/span>\n\n<span class=\"hljs-keyword\">let<\/span> luckyNumbers = &#91;<span class=\"hljs-number\">100<\/span>, <span class=\"hljs-number\">40<\/span>, <span class=\"hljs-number\">10<\/span>, <span class=\"hljs-number\">300<\/span>, <span class=\"hljs-number\">20<\/span>, <span class=\"hljs-number\">50<\/span>, <span class=\"hljs-number\">30<\/span>];\n<span class=\"hljs-built_in\">console<\/span>.log(_.sortBy(luckyNumbers));\n\n<span class=\"hljs-comment\">\/\/ returns: &#91;10, 20, 30, 40, 50, 100, 300]<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-19\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>You&#8217;ll also notice that we didn&#8217;t need to write any sorting logic for large numbers!<\/p>\n\n\n\n<p>Additionally, in the case of our array of objects, where we sorted the top JavaScript libraries example from earlier, we could simply pass our preferred sorting key as follows:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-20\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-comment\">\/\/ sort by name<\/span>\n<span class=\"hljs-built_in\">console<\/span>.log(_.sortBy(jsLibraries, <span class=\"hljs-string\">\"name\"<\/span>));\n\n<span class=\"hljs-comment\">\/\/ sort by star<\/span>\n<span class=\"hljs-built_in\">console<\/span>.log(_.sortBy(jsLibraries, <span class=\"hljs-string\">\"star\"<\/span>));<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-20\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<h3 class=\"wp-block-heading\">How to do complex sorting using Lodash&#8217;s _.orderBy()<\/h3>\n\n\n\n<p>Unfortunately, the <code>_.sortBy()<\/code> method can only be used to sort arrays in ascending order. However, <code>lodash<\/code> includes an additional <code>_.orderBy()<\/code> method that allows for more flexibility. Its syntax is pretty similar to&nbsp; <code>_.sortBy()<\/code>, except that it accepts an optional third parameter that lets us specify the sort order:<br><\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-21\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">const<\/span> _ = <span class=\"hljs-built_in\">require<\/span>(<span class=\"hljs-string\">\"lodash\"<\/span>);\n<span class=\"hljs-keyword\">let<\/span> jsLibraries = &#91;\n  {\n    <span class=\"hljs-attr\">name<\/span>: <span class=\"hljs-string\">\"React\"<\/span>,\n    <span class=\"hljs-attr\">star<\/span>: <span class=\"hljs-number\">194478<\/span>,\n  },\n  {\n    <span class=\"hljs-attr\">name<\/span>: <span class=\"hljs-string\">\"Vue\"<\/span>,\n    <span class=\"hljs-attr\">star<\/span>: <span class=\"hljs-number\">199199<\/span>,\n  },\n  {\n    <span class=\"hljs-attr\">name<\/span>: <span class=\"hljs-string\">\"Angular\"<\/span>,\n    <span class=\"hljs-attr\">star<\/span>: <span class=\"hljs-number\">83716<\/span>,\n  },\n  {\n    <span class=\"hljs-attr\">name<\/span>: <span class=\"hljs-string\">\"Lodash\"<\/span>,\n    <span class=\"hljs-attr\">star<\/span>: <span class=\"hljs-number\">54288<\/span>,\n  },\n];\n\n<span class=\"hljs-built_in\">console<\/span>.log(_.orderBy(jsLibraries, <span class=\"hljs-string\">\"name\"<\/span>, <span class=\"hljs-string\">\"desc\"<\/span>));\n\n<span class=\"hljs-comment\">\/*\nreturns:\n&#91;\n  { name: 'Vue', star: 199199 },\n  { name: 'React', star: 194478 },\n  { name: 'Lodash', star: 54288 },\n  { name: 'Angular', star: 83716 }\n]\n*\/<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-21\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Additionally, we could specify a different sort order for different object keys in a single sort:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-22\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">const<\/span> _ = <span class=\"hljs-built_in\">require<\/span>(<span class=\"hljs-string\">\"lodash\"<\/span>);\n<span class=\"hljs-keyword\">var<\/span> users = &#91;\n\u00a0 { <span class=\"hljs-attr\">name<\/span>: <span class=\"hljs-string\">\"Alex\"<\/span>, <span class=\"hljs-attr\">age<\/span>: <span class=\"hljs-number\">48<\/span> },\n\u00a0 { <span class=\"hljs-attr\">name<\/span>: <span class=\"hljs-string\">\"Jane\"<\/span>, <span class=\"hljs-attr\">age<\/span>: <span class=\"hljs-number\">34<\/span> },\n\u00a0 { <span class=\"hljs-attr\">name<\/span>: <span class=\"hljs-string\">\"Alex\"<\/span>, <span class=\"hljs-attr\">age<\/span>: <span class=\"hljs-number\">40<\/span> },\n\u00a0 { <span class=\"hljs-attr\">name<\/span>: <span class=\"hljs-string\">\"Jane\"<\/span>, <span class=\"hljs-attr\">age<\/span>: <span class=\"hljs-number\">36<\/span> },\n];\n\n<span class=\"hljs-built_in\">console<\/span>.log(_.orderBy(users, &#91;<span class=\"hljs-string\">\"name\"<\/span>, <span class=\"hljs-string\">\"age\"<\/span>], &#91;<span class=\"hljs-string\">\"asc\"<\/span>, <span class=\"hljs-string\">\"desc\"<\/span>]));\n\n<span class=\"hljs-comment\">\/*\nreturns:\n&#91;\n\u00a0 { name: 'Alex', age: 48 },\n\u00a0 { name: 'Alex', age: 40 },\n\u00a0 { name: 'Jane', age: 36 },\n\u00a0 { name: 'Jane', age: 34 }\n]\n*\/<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-22\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>In the example above, we&#8217;ve used the <code>_.orderBy()<\/code> method to sort our array by name in ascending order and age in descending order.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Comparing performance<\/h2>\n\n\n\n<p>To compare the performance of the native JavaScript <code>.sort()<\/code> method and the <code>lodash<\/code> <code>_.sortBy()<\/code> method, I created a simple benchmark test using our previous top JavaScript libraries array. The results: the native JavaScript method performed significantly better than <code>lodash<\/code>.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/09\/img_6328e905a3213.png\" target=\"_blank\" rel=\"noopener\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/09\/img_6328e905a3213.png\" alt=\"\"\/><\/a><figcaption>A benchmark of the native comparison function versus the lodash method of sortBy. The lodash method is 81.47% slower. <a href=\"https:\/\/jsbench.me\/hfl7uxywf1\/1\" target=\"_blank\" rel=\"noreferrer noopener\">Benchmark hosted on JSBench.ME<\/a><\/figcaption><\/figure>\n\n\n\n<p>This is not surprising given that Lodash&#8217;s method is loaded from a third-party library rather than being built directly into the JavaScript engine. Its approach also degrades performance because, unlike the native <code>.sort()<\/code> method, it does not directly manipulate the original array but creates a modified copy.<\/p>\n\n\n\n<p>To make the comparison more fair, I performed a second test with the same array, but this time using the spread operator to copy the original array before sorting with the <code>.sort()<\/code> method.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><a href=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/09\/img_6328e9074da8d.png\" target=\"_blank\" rel=\"noopener\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"374\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/09\/img_6328e9074da8d-1024x374.png\" alt=\"\" class=\"wp-image-18962\" srcset=\"https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_6328e9074da8d-1024x374.png 1024w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_6328e9074da8d-300x110.png 300w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_6328e9074da8d-768x280.png 768w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_6328e9074da8d-1536x561.png 1536w, https:\/\/coderpad.io\/wp-content\/uploads\/2022\/09\/img_6328e9074da8d.png 1600w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/a><figcaption>The native benchmark now uses the spread operator to create a new array. Lodash is now only 68% slower than native. <a href=\"https:\/\/jsbench.me\/a5l88vch8c\/1\" target=\"_blank\" rel=\"noreferrer noopener\">Benchmark hosted on JSBench.ME<\/a><\/figcaption><\/figure>\n\n\n\n<p>While the native <code>.sort()<\/code> method remains significantly faster, the slowness of the <code>lodash _.sortBy()<\/code> method has decreased from 81% to 68%.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>This tutorial taught us how to use the <code>.sort()<\/code> method in JavaScript while experimenting with various data structures. We also covered how to do equivalent sorting with the <code>lodash<\/code> <code>_.sortBy()<\/code> and <code>_.orderBy()<\/code> methods and finally compared the performance of the two options, with the JS native sort outperforming the latter.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sorting in JavaScript is an operation to keep items in the correct order. There are several ways to sort data types such as integers, strings, and arrays, and this article walks you through these methods.<\/p>\n","protected":false},"author":1,"featured_media":19182,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[9],"tags":[],"persona":[29],"blog-programming-language":[33],"keyword-cluster":[],"class_list":["post-18960","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development"],"acf":[],"_links":{"self":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/18960","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/comments?post=18960"}],"version-history":[{"count":33,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/18960\/revisions"}],"predecessor-version":[{"id":20989,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/18960\/revisions\/20989"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/media\/19182"}],"wp:attachment":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/media?parent=18960"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/categories?post=18960"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/tags?post=18960"},{"taxonomy":"persona","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/persona?post=18960"},{"taxonomy":"blog-programming-language","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/blog-programming-language?post=18960"},{"taxonomy":"keyword-cluster","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/keyword-cluster?post=18960"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}