<p>I have a list of objects I wish to sort based on a field <code>attr</code> of type string. I tried using <code>-</code></p>

<pre><code>list.sort(function (a, b) {
    return a.attr - b.attr
})
</code></pre>

<p>but found that <code>-</code> doesn't appear to work with strings in JavaScript. How can I sort a list of objects based on an attribute with type string?</p>

[{'answer_id': 51169, 'author': 'Shog9', 'author_id': 811, 'author_profile': 'https://Stackoverflow.com/users/811', 'pm_score': 11, 'selected': True, 'text': '<p>Use <a href="https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/String/localeCompare" rel="noreferrer"><code>String.prototype.localeCompare</code></a> a per your example:</p>\n\n<pre><code>list.sort(function (a, b) {\n    return (\'\' + a.attr).localeCompare(b.attr);\n})\n</code></pre>\n\n<p>We force a.attr to be a string to avoid exceptions. <code>localeCompare</code> has been supported <a href="https://learn.microsoft.com/en-us/scripting/javascript/reference/localecompare-method-string-javascript" rel="noreferrer">since Internet Explorer 6</a> and Firefox 1. You may also see the following code used that doesn\'t respect a locale:</p>\n\n<pre><code>if (item1.attr &lt; item2.attr)\n  return -1;\nif ( item1.attr &gt; item2.attr)\n  return 1;\nreturn 0;\n</code></pre>\n'}, {'answer_id': 51170, 'author': 'airportyh', 'author_id': 5304, 'author_profile': 'https://Stackoverflow.com/users/5304', 'pm_score': 4, 'selected': False, 'text': '<p>You should use > or &lt; and == here. So the solution would be:</p>\n\n<pre><code>list.sort(function(item1, item2) {\n    var val1 = item1.attr,\n        val2 = item2.attr;\n    if (val1 == val2) return 0;\n    if (val1 &gt; val2) return 1;\n    if (val1 &lt; val2) return -1;\n});\n</code></pre>\n'}, {'answer_id': 14757938, 'author': 'Manav', 'author_id': 141220, 'author_profile': 'https://Stackoverflow.com/users/141220', 'pm_score': 3, 'selected': False, 'text': '<p>I had been bothered about this for long, so I finally researched this and give you this long winded reason for why things are the way they are.</p>\n\n<p>From the <a href="http://ecma262-5.com/ELS5_HTML.htm" rel="noreferrer">spec</a>:</p>\n\n<pre><code>Section 11.9.4   The Strict Equals Operator ( === )\n\nThe production EqualityExpression : EqualityExpression === RelationalExpression\nis evaluated as follows: \n- Let lref be the result of evaluating EqualityExpression.\n- Let lval be GetValue(lref).\n- Let rref be the result of evaluating RelationalExpression.\n- Let rval be GetValue(rref).\n- Return the result of performing the strict equality comparison \n  rval === lval. (See 11.9.6)\n</code></pre>\n\n<p>So now we go to 11.9.6</p>\n\n<pre><code>11.9.6   The Strict Equality Comparison Algorithm\n\nThe comparison x === y, where x and y are values, produces true or false. \nSuch a comparison is performed as follows: \n- If Type(x) is different from Type(y), return false.\n- If Type(x) is Undefined, return true.\n- If Type(x) is Null, return true.\n- If Type(x) is Number, then\n...\n- If Type(x) is String, then return true if x and y are exactly the \n  same sequence of characters (same length and same characters in \n  corresponding positions); otherwise, return false.\n</code></pre>\n\n<p>That\'s it. <strong>The triple equals operator applied to strings returns true iff the arguments are  exactly the same strings (same length and same characters in corresponding positions).</strong></p>\n\n<p>So <code>===</code> will work in the cases when we\'re trying to compare strings which might have arrived from different sources, but which we know will eventually have the same values - a common enough scenario for inline strings in our code. For example, if we have a variable named <code>connection_state</code>, and we wish to know which one of the following states <code>[\'connecting\', \'connected\', \'disconnecting\', \'disconnected\']</code> is it in right now, we can directly use the <code>===</code>. </p>\n\n<p>But there\'s more. Just above 11.9.4, there is a short note:</p>\n\n<pre><code>NOTE 4     \n  Comparison of Strings uses a simple equality test on sequences of code \n  unit values. There is no attempt to use the more complex, semantically oriented\n  definitions of character or string equality and collating order defined in the \n  Unicode specification. Therefore Strings values that are canonically equal\n  according to the Unicode standard could test as unequal. In effect this \n  algorithm assumes that both Strings are already in normalized form.\n</code></pre>\n\n<p>Hmm. What now? Externally obtained strings can, and most likely will, be weird unicodey, and our gentle <code>===</code> won\'t do them justice. In comes <code>localeCompare</code> to the rescue:</p>\n\n<pre><code>15.5.4.9   String.prototype.localeCompare (that)\n    ...\n    The actual return values are implementation-defined to permit implementers \n    to encode additional information in the value, but the function is required \n    to define a total ordering on all Strings and to return 0 when comparing\n    Strings that are considered canonically equivalent by the Unicode standard. \n</code></pre>\n\n<p>We can go home now.</p>\n\n<p><strong>tl;dr;</strong></p>\n\n<p>To compare strings in javascript, use <code>localeCompare</code>; if you know that the strings have no non-ASCII components because they are, for example, internal program constants, then <code>===</code> also works.</p>\n'}, {'answer_id': 19573507, 'author': 'eggmatters', 'author_id': 1010444, 'author_profile': 'https://Stackoverflow.com/users/1010444', 'pm_score': 0, 'selected': False, 'text': '<p>In your operation in your initial question, you are performing the following operation:</p>\n\n<pre><code>item1.attr - item2.attr\n</code></pre>\n\n<p>So, assuming those are numbers (i.e. item1.attr = "1", item2.attr = "2") You still may use the "===" operator (or other strict evaluators) provided that you ensure type. The following should work:</p>\n\n<pre><code>return parseInt(item1.attr) - parseInt(item2.attr);\n</code></pre>\n\n<p>If they are alphaNumeric, then do use localCompare().</p>\n'}, {'answer_id': 26295229, 'author': 'Adrien Be', 'author_id': 759452, 'author_profile': 'https://Stackoverflow.com/users/759452', 'pm_score': 8, 'selected': False, 'text': '<h2>An updated answer (October 2014)</h2>\n\n<p>I was really annoyed about this string natural sorting order so I took quite some time to investigate this issue. I hope this helps.</p>\n\n<h3>Long story short</h3>\n\n<p><code>localeCompare()</code> character support is badass, just use it.\nAs pointed out by <code>Shog9</code>, the answer to your question is:</p>\n\n<pre><code>return item1.attr.localeCompare(item2.attr);\n</code></pre>\n\n<h3>Bugs found in all the custom javascript "natural string sort order" implementations</h3>\n\n<p>There are quite a bunch of custom implementations out there, trying to do string comparison more precisely called "natural string sort order"</p>\n\n<p>When "playing" with these implementations, I always noticed some strange "natural sorting order" choice, or rather mistakes (or omissions in the best cases).</p>\n\n<p>Typically, special characters (space, dash, ampersand, brackets, and so on) are not processed correctly.</p>\n\n<p>You will then find them appearing mixed up in different places, typically that could be:</p>\n\n<ul>\n<li>some will be between the uppercase \'Z\' and the lowercase \'a\' </li>\n<li>some will be between the \'9\' and the uppercase \'A\'</li>\n<li>some will be after lowercase \'z\'</li>\n</ul>\n\n<p>When one would have expected special characters to all be "grouped" together in one place, except for the space special character maybe (which would always be the first character). That is, either all before numbers, or all between numbers and letters (lowercase &amp; uppercase being "together" one after another), or all after letters.</p>\n\n<p>My conclusion is that they all fail to provide a consistent order when I start adding barely unusual characters (ie. characters with diacritics or charcters such as dash, exclamation mark and so on).</p>\n\n<p>Research on the custom implementations:</p>\n\n<ul>\n<li><code>Natural Compare Lite</code> <a href="https://github.com/litejs/natural-compare-lite" rel="noreferrer">https://github.com/litejs/natural-compare-lite</a> : Fails at sorting consistently <a href="https://github.com/litejs/natural-compare-lite/issues/1" rel="noreferrer">https://github.com/litejs/natural-compare-lite/issues/1</a> and <a href="http://jsbin.com/bevututodavi/1/edit?js,console" rel="noreferrer">http://jsbin.com/bevututodavi/1/edit?js,console</a> , basic latin characters sorting <a href="http://jsbin.com/bevututodavi/5/edit?js,console" rel="noreferrer">http://jsbin.com/bevututodavi/5/edit?js,console</a> </li>\n<li><code>Natural Sort</code> <a href="https://github.com/javve/natural-sort" rel="noreferrer">https://github.com/javve/natural-sort</a> : Fails at sorting consistently, see issue <a href="https://github.com/javve/natural-sort/issues/7" rel="noreferrer">https://github.com/javve/natural-sort/issues/7</a> and see basic latin characters sorting <a href="http://jsbin.com/cipimosedoqe/3/edit?js,console" rel="noreferrer">http://jsbin.com/cipimosedoqe/3/edit?js,console</a> </li>\n<li><code>Javascript Natural Sort</code> <a href="https://github.com/overset/javascript-natural-sort" rel="noreferrer">https://github.com/overset/javascript-natural-sort</a> : seems rather neglected since February 2012, Fails at sorting consistently, see issue <a href="https://github.com/overset/javascript-natural-sort/issues/16" rel="noreferrer">https://github.com/overset/javascript-natural-sort/issues/16</a></li>\n<li><code>Alphanum</code> <a href="http://www.davekoelle.com/files/alphanum.js" rel="noreferrer">http://www.davekoelle.com/files/alphanum.js</a> , Fails at sorting consistently, see <a href="http://jsbin.com/tuminoxifuyo/1/edit?js,console" rel="noreferrer">http://jsbin.com/tuminoxifuyo/1/edit?js,console</a></li>\n</ul>\n\n<h3>Browsers\' native "natural string sort order" implementations via <code>localeCompare()</code></h3>\n\n<p><code>localeCompare()</code> oldest implementation (without the locales and options arguments) is supported by IE6+, see <a href="http://msdn.microsoft.com/en-us/library/ie/s4esdbwz(v=vs.94).aspx" rel="noreferrer">http://msdn.microsoft.com/en-us/library/ie/s4esdbwz(v=vs.94).aspx</a> (scroll down to localeCompare() method).\nThe built-in <code>localeCompare()</code> method does a much better job at sorting, even international &amp; special characters.\nThe only problem using the <code>localeCompare()</code> method is that <a href="https://code.google.com/p/v8/issues/detail?id=459" rel="noreferrer">"the locale and sort order used are entirely implementation dependent". In other words, when using localeCompare such as stringOne.localeCompare(stringTwo): Firefox, Safari, Chrome &amp; IE have a different sort order for Strings.</a></p>\n\n<p>Research on the browser-native implementations:</p>\n\n<ul>\n<li><a href="http://jsbin.com/beboroyifomu/1/edit?js,console" rel="noreferrer">http://jsbin.com/beboroyifomu/1/edit?js,console</a> - basic latin characters comparison with localeCompare()\n<a href="http://jsbin.com/viyucavudela/2/" rel="noreferrer">http://jsbin.com/viyucavudela/2/</a> - basic latin characters comparison with localeCompare() for testing on IE8</li>\n<li><a href="http://jsbin.com/beboroyifomu/2/edit?js,console" rel="noreferrer">http://jsbin.com/beboroyifomu/2/edit?js,console</a> - basic latin characters in string comparison : consistency check in string vs when a character is alone</li>\n<li><a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare" rel="noreferrer">https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/localeCompare</a> - IE11+ supports the new locales &amp; options arguments</li>\n</ul>\n\n<h3>Difficulty of "string natural sorting order"</h3>\n\n<p>Implementing a solid algorithm (meaning: consistent but also covering a wide range of characters) is a very tough task. UTF8 contains <a href="http://en.wikipedia.org/wiki/UTF-8" rel="noreferrer">more than 2000 characters</a> &amp; <a href="http://www.unicode.org/standard/supported.html" rel="noreferrer">covers more than 120 scripts (languages)</a>. \nFinally, there are some specification for this tasks, it is called the "Unicode Collation Algorithm", which can be found at <a href="http://www.unicode.org/reports/tr10/" rel="noreferrer">http://www.unicode.org/reports/tr10/</a> . You can find more information about this on this question I posted <a href="https://softwareengineering.stackexchange.com/questions/257286/is-there-any-language-agnostic-specification-for-string-natural-sorting-order">https://softwareengineering.stackexchange.com/questions/257286/is-there-any-language-agnostic-specification-for-string-natural-sorting-order</a></p>\n\n<h2>Final conclusion</h2>\n\n<p>So considering the current level of support provided by the javascript custom implementations I came across, we will probably never see anything getting any close to supporting all this characters &amp; scripts (languages). Hence I would rather use the browsers\' native localeCompare() method. Yes, it does have the downside of beeing non-consistent across browsers but basic testing shows it covers a much wider range of characters, allowing solid &amp; meaningful sort orders.</p>\n\n<p>So as pointed out by <code>Shog9</code>, the answer to your question is:</p>\n\n<pre><code>return item1.attr.localeCompare(item2.attr);\n</code></pre>\n\n<h3>Further reading:</h3>\n\n<ul>\n<li><a href="https://softwareengineering.stackexchange.com/questions/257286/is-there-any-language-agnostic-specification-for-string-natural-sorting-order">https://softwareengineering.stackexchange.com/questions/257286/is-there-any-language-agnostic-specification-for-string-natural-sorting-order</a></li>\n<li><a href="https://stackoverflow.com/questions/51165/how-do-you-do-string-comparison-in-javascript">How do you do string comparison in JavaScript?</a></li>\n<li><a href="https://stackoverflow.com/questions/2802341/natural-sort-of-text-and-numbers-javascript">Javascript : natural sort of alphanumerical strings</a></li>\n<li><a href="https://stackoverflow.com/questions/4373018/sort-array-of-numeric-alphabetical-elements-natural-sort/">Sort Array of numeric &amp; alphabetical elements (Natural Sort)</a></li>\n<li><a href="https://stackoverflow.com/questions/4340227/sort-mixed-alpha-numeric-array">Sort mixed alpha/numeric array</a></li>\n<li><a href="https://web.archive.org/web/20130929122019/http://my.opera.com/GreyWyvern/blog/show.dml/1671288" rel="noreferrer">https://web.archive.org/web/20130929122019/http://my.opera.com/GreyWyvern/blog/show.dml/1671288</a></li>\n<li><a href="https://web.archive.org/web/20131005224909/http://www.davekoelle.com/alphanum.html" rel="noreferrer">https://web.archive.org/web/20131005224909/http://www.davekoelle.com/alphanum.html</a></li>\n<li><a href="http://snipplr.com/view/36012/javascript-natural-sort/" rel="noreferrer">http://snipplr.com/view/36012/javascript-natural-sort/</a></li>\n<li><a href="http://blog.codinghorror.com/sorting-for-humans-natural-sort-order/" rel="noreferrer">http://blog.codinghorror.com/sorting-for-humans-natural-sort-order/</a></li>\n</ul>\n\n<p>Thanks to Shog9\'s nice answer, which put me in the "right" direction I believe</p>\n'}, {'answer_id': 39281302, 'author': 'Petr Varyagin', 'author_id': 5149578, 'author_profile': 'https://Stackoverflow.com/users/5149578', 'pm_score': 1, 'selected': False, 'text': "<pre><code>list.sort(function(item1, item2){\n    return +(item1.attr &gt; item2.attr) || +(item1.attr === item2.attr) - 1;\n}) \n</code></pre>\n\n<p>How they work samples:</p>\n\n<pre><code>+('aaa'&gt;'bbb')||+('aaa'==='bbb')-1\n+(false)||+(false)-1\n0||0-1\n-1\n\n+('bbb'&gt;'aaa')||+('bbb'==='aaa')-1\n+(true)||+(false)-1\n1||0-1\n1\n\n+('aaa'&gt;'aaa')||+('aaa'==='aaa')-1\n+(false)||+(true)-1\n0||1-1\n0\n</code></pre>\n"}, {'answer_id': 40355107, 'author': 'mpyw', 'author_id': 1846562, 'author_profile': 'https://Stackoverflow.com/users/1846562', 'pm_score': 6, 'selected': False, 'text': '<h1>Answer (in Modern ECMAScript)</h1>\n\n<pre><code>list.sort((a, b) =&gt; (a.attr &gt; b.attr) - (a.attr &lt; b.attr))\n</code></pre>\n\n<p>Or </p>\n\n<pre><code>list.sort((a, b) =&gt; +(a.attr &gt; b.attr) || -(a.attr &lt; b.attr))\n</code></pre>\n\n<h1>Description</h1>\n\n<p>Casting a boolean value to a number yields the following:</p>\n\n<ul>\n<li><code>true</code> -> <code>1</code></li>\n<li><code>false</code> -> <code>0</code></li>\n</ul>\n\n<p>Consider three possible patterns:</p>\n\n<ul>\n<li>x is larger than y: <code>(x &gt; y) - (y &lt; x)</code> -> <code>1 - 0</code> -> <code>1</code></li>\n<li>x is equal to y: <code>(x &gt; y) - (y &lt; x)</code> -> <code>0 - 0</code> -> <code>0</code></li>\n<li>x is smaller than y: <code>(x &gt; y) - (y &lt; x)</code> -> <code>0 - 1</code> -> <code>-1</code></li>\n</ul>\n\n<p>(Alternative)</p>\n\n<ul>\n<li>x is larger than y: <code>+(x &gt; y) || -(x &lt; y)</code> -> <code>1 || 0</code> -> <code>1</code></li>\n<li>x is equal to y: <code>+(x &gt; y) || -(x &lt; y)</code> -> <code>0 || 0</code> -> <code>0</code></li>\n<li>x is smaller than y: <code>+(x &gt; y) || -(x &lt; y)</code> -> <code>0 || -1</code> -> <code>-1</code></li>\n</ul>\n\n<p>So these logics are equivalent to typical sort comparator functions.</p>\n\n<pre><code>if (x == y) {\n    return 0;\n}\nreturn x &gt; y ? 1 : -1;\n</code></pre>\n'}, {'answer_id': 49989678, 'author': 'Julio Munoz', 'author_id': 4122286, 'author_profile': 'https://Stackoverflow.com/users/4122286', 'pm_score': -1, 'selected': False, 'text': '<pre><code>&lt;!doctype html&gt;\n&lt;html&gt;\n&lt;body&gt;\n&lt;p id = "myString"&gt;zyxtspqnmdba&lt;/p&gt;\n&lt;p id = "orderedString"&gt;&lt;/p&gt;\n&lt;script&gt;\nvar myString = document.getElementById("myString").innerHTML;\norderString(myString);\nfunction orderString(str) {\n    var i = 0;\n    var myArray = str.split("");\n    while (i &lt; str.length){\n        var j = i + 1;\n        while (j &lt; str.length) {\n            if (myArray[j] &lt; myArray[i]){\n                var temp = myArray[i];\n                myArray[i] = myArray[j];\n                myArray[j] = temp;\n            }\n            j++;\n        }\n        i++;\n    }\n    var newString = myArray.join("");\n    document.getElementById("orderedString").innerHTML = newString;\n}\n&lt;/script&gt;\n&lt;/body&gt;\n&lt;/html&gt;\n</code></pre>\n'}, {'answer_id': 55153827, 'author': 'geckos', 'author_id': 652528, 'author_profile': 'https://Stackoverflow.com/users/652528', 'pm_score': 4, 'selected': False, 'text': '<p>Nested ternary arrow function</p>\n\n<pre><code>(a,b) =&gt; (a &lt; b ? -1 : a &gt; b ? 1 : 0)\n</code></pre>\n'}, {'answer_id': 55590696, 'author': 'Abdul', 'author_id': 9359891, 'author_profile': 'https://Stackoverflow.com/users/9359891', 'pm_score': -1, 'selected': False, 'text': "<pre><code>var str = ['v','a','da','c','k','l']\nvar b = str.join('').split('').sort().reverse().join('')\nconsole.log(b)\n</code></pre>\n"}, {'answer_id': 58049712, 'author': 'Alejadro Xalabarder', 'author_id': 1191101, 'author_profile': 'https://Stackoverflow.com/users/1191101', 'pm_score': 5, 'selected': False, 'text': '<p>since strings can be compared directly in javascript, this will do the job</p>\n\n<pre><code>list.sort(function (a, b) {\n    return a.attr &gt; b.attr ? 1: -1;\n})\n</code></pre>\n\n<p>the subtraction in a sort function is used only when non alphabetical (numerical) sort is desired and of course it does not work with strings</p>\n'}, {'answer_id': 64611883, 'author': 'tash', 'author_id': 11932012, 'author_profile': 'https://Stackoverflow.com/users/11932012', 'pm_score': 3, 'selected': False, 'text': '<p>An explanation of why the approach in the question doesn\'t work:</p>\n<pre class="lang-js prettyprint-override"><code>let products = [\n    { name: &quot;laptop&quot;, price: 800 },\n    { name: &quot;phone&quot;, price:200},\n    { name: &quot;tv&quot;, price: 1200}\n];\nproducts.sort( (a, b) =&gt; {\n    {let value= a.name - b.name; console.log(value); return value}\n});\n\n&gt; 2 NaN\n</code></pre>\n<p>Subtraction between strings returns NaN.</p>\n<p>Echoing @Alejadro\'s answer, the right approach is--</p>\n<p><code>products.sort( (a,b) =&gt; a.name &gt; b.name ? 1 : -1 )</code></p>\n'}, {'answer_id': 65979128, 'author': 'TrickOrTreat', 'author_id': 11502061, 'author_profile': 'https://Stackoverflow.com/users/11502061', 'pm_score': 2, 'selected': False, 'text': '<p>Use sort() straight forward without any <code>-</code> or <code>&lt;</code></p>\n<p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">\r\n<div class="snippet-code">\r\n<pre class="snippet-code-js lang-js prettyprint-override"><code>const areas = [\'hill\', \'beach\', \'desert\', \'mountain\']\nconsole.log(areas.sort())\n\n// To print in descending way\nconsole.log(areas.sort().reverse())</code></pre>\r\n</div>\r\n</div>\r\n</p>\n'}, {'answer_id': 68123055, 'author': 'deathfry', 'author_id': 13029968, 'author_profile': 'https://Stackoverflow.com/users/13029968', 'pm_score': 2, 'selected': False, 'text': "<p>There should be ascending and descending orders functions</p>\n<pre><code>if (order === 'asc') {\n  return a.localeCompare(b);\n}\nreturn b.localeCompare(a);\n</code></pre>\n"}, {'answer_id': 70430101, 'author': 'Felix Orinda', 'author_id': 13779529, 'author_profile': 'https://Stackoverflow.com/users/13779529', 'pm_score': 3, 'selected': False, 'text': "<p>In javascript, we use the <code>.localCompare</code> method to sort strings</p>\n<p>For Typescript</p>\n<pre><code>const data = [&quot;jane&quot;, &quot;mike&quot;, &quot;salome&quot;, &quot;ababus&quot;, &quot;buisa&quot;, &quot;dennis&quot;];\n\nconst sort = (arr: string[], mode?: 'desc' | 'asc') =&gt; \n  !mode || mode === &quot;asc&quot;\n    ? arr.sort((a, b) =&gt; a.localeCompare(b))\n    : arr.sort((a, b) =&gt; b.localeCompare(a));\n\n\nconsole.log(sort(data, 'desc'));// [ 'salome', 'mike', 'jane', 'dennis', 'buisa', 'ababus' ]\nconsole.log(sort(data, 'asc')); // [ 'ababus', 'buisa', 'dennis', 'jane', 'mike', 'salome' ]\n\n\n</code></pre>\n<p>For JS</p>\n<pre><code>const data = [&quot;jane&quot;, &quot;mike&quot;, &quot;salome&quot;, &quot;ababus&quot;, &quot;buisa&quot;, &quot;dennis&quot;];\n\n/**\n * @param {string[]} arr\n * @param {&quot;asc&quot;|&quot;desc&quot;} mode\n */\nconst sort = (arr, mode = &quot;asc&quot;) =&gt;\n  !mode || mode === &quot;asc&quot;\n    ? arr.sort((a, b) =&gt; a.localeCompare(b))\n    : arr.sort((a, b) =&gt; b.localeCompare(a));\n\nconsole.log(sort(data, 'desc'));// [ 'salome', 'mike', 'jane', 'dennis', 'buisa', 'ababus' ]\nconsole.log(sort(data, 'asc')); // [ 'ababus', 'buisa', 'dennis', 'jane', 'mike', 'salome' ]\n</code></pre>\n"}, {'answer_id': 70890849, 'author': 'xmedeko', 'author_id': 254109, 'author_profile': 'https://Stackoverflow.com/users/254109', 'pm_score': 2, 'selected': False, 'text': '<p>If you want to control locales (or case or accent), then use <a href="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl/Collator/Collator" rel="nofollow noreferrer"><code>Intl.collator</code></a>:</p>\n<pre class="lang-js prettyprint-override"><code>const collator = new Intl.Collator();\nlist.sort((a, b) =&gt; collator.compare(a.attr, b.attr));\n</code></pre>\n<p>You can construct collator like:</p>\n<pre class="lang-js prettyprint-override"><code>new Intl.Collator(&quot;en&quot;);\nnew Intl.Collator(&quot;en&quot;, {sensitivity: &quot;case&quot;});\n...\n</code></pre>\n<p>see the above link for doc.</p>\n<p>Note: unlike some other solutions, it handles <code>null, undefined</code> the JS way, i.e. moves them to the end.</p>\n'}]
['https://Stackoverflow.com/questions/51165', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5304/']