2

Does Array.sort() fail on large arrays? Or did I miss something? I also checked that I'm not sorting strings by mistake. I can't seem to understand why this fails

Pasting directly from Chrome Debugger's Watch:

enter image description here

And here's something you can play with:

arr = [1.238648000000012,1.5776880000000233,1.6462280000000078,10.007896000000017,10.014455999999996,10.016970000000015,10.018888000000004,10.028069000000016,10.047926000000018,10.056818000000021,10.057980999999984,10.075353000000007,10.086242000000027,10.088684999999998,10.097217,10.102441999999996,10.104630999999983,10.107723000000021,10.108536000000015,10.115552999999977,10.137179000000003,10.171389999999974,10.203172999999992,10.208004000000017,10.217614000000026,10.231874000000005,10.241456000000028,10.243750999999975,10.252985000000024,10.255207999999982,10.27183500000001,10.30159500000002,10.342756000000008,10.35523599999999,10.364777000000004,10.391841,10.40662900000001,10.422856000000024,10.428019000000006,10.438702999999975,10.493548999999973,10.495686999999975,10.522063000000003,10.541649000000007,10.577163999999982,10.605306999999982,10.614864000000011,10.63799899999998,10.677408000000014,10.677971000000014,10.678615999999977,10.683666000000017,10.697460999999976,10.702691000000016,10.738349000000028,10.748320999999976,10.783839,10.834334000000013,10.843086000000028,10.862792000000013,10.893118000000015,10.905455000000018,10.906916000000024,10.923288000000014,10.958798999999999,10.999311999999975,11.003940999999998,11.01112999999998,11.011986999999976,11.022662000000025,11.030069000000026,11.033493000000021,11.06088699999998,11.089207999999985,11.120248000000004,11.169981000000007,11.22742599999998,11.373028999999974,11.38974300000001,11.406037000000026,11.422123,11.445996999999977,11.445996999999977,11.452351000000021,11.45395400000001,11.455066999999985,11.530115000000023,11.563641000000018,11.649070999999992,11.681149000000005,11.699284999999975,11.715215,11.786181,11.798755000000028,11.912154999999984,11.914702999999975,11.92000999999999,11.925179000000014,11.989391000000012,12.065800000000024,12.077261000000021,12.144492000000014,12.213693999999975,12.22724199999999,12.244993000000022,12.293795999999986,12.337166000000025,12.366329000000007,12.42165799999998,12.511757999999986,12.57682699999998,12.583461,12.612408000000016,12.616739999999993,12.662854999999979,12.663344999999993,12.663866999999982,12.673579000000018,12.727368000000013,12.738046999999995,12.748396000000014,12.768007000000011,12.853508999999974,12.860353999999973,12.884284999999977,12.900110999999981,12.92121800000001,12.976439000000028,12.98437100000001,13.01056299999999,13.012036000000023,13.055876000000012,13.138213000000007,13.13959699999998,13.147134999999992,13.156939000000023,13.171355000000005,13.223600999999974,13.240787000000012,13.26833499999998,13.271131999999994,13.272153000000003,13.291954999999973,13.307935999999984,13.41423500000002,13.417280000000005,13.43734999999998,13.448892,13.489640000000009,13.490972,13.491602999999998,13.502335000000016,13.532481000000018,13.55182000000002,13.555890999999974,13.578921999999977,13.580539999999985,13.652214000000015,13.662713999999994,13.674057000000005,13.722780999999998,13.781020000000012,13.790301,13.801070999999979,13.801070999999979,13.826026000000013,13.843449000000021,13.974924999999985,14.012158,14.02240599999999,14.041520999999989,14.088280999999995,14.189909,14.226448000000005,14.288890999999978,14.298192000000029,14.36671100000001,14.379841999999996,14.459216000000026,14.471633999999995,14.51484099999999,14.527475999999979,14.538589000000002,14.608111000000008,14.72200700000002,14.750081000000023,14.818646999999999,14.847937000000002,14.85942,14.874291000000028,14.987093000000016,15.072758000000022,15.126066999999978,15.220902000000024,15.24462699999998,15.323228000000029,15.434033999999997,15.44965000000002,15.45765799999998,15.519754999999975,15.53459700000002,15.599244999999996,15.611743999999987,15.636731999999995,15.672317000000021,15.752161999999998,15.753962999999999,15.835087999999985,16.102216,16.126821000000007,16.12912,16.158558000000028,16.169849999999997,16.178314,16.188782000000003,16.251914,16.32066900000001,16.33669900000001,16.339541999999994,16.38289900000001,16.408960999999977,16.430653000000007,16.437236999999982,16.44171399999999,16.506533999999988,16.60108200000002,16.630889000000025,16.63222300000001,16.64343200000002,16.661643000000026,16.66319199999998,16.71718199999998,16.729517999999985,16.786464000000024,16.796617000000026,16.80394100000001,16.80713800000001,16.811587999999972,16.82970899999998,16.83151700000002,16.854597000000012,16.913752999999986,16.96798100000001,17.025273000000027,17.02839799999998,17.03959900000001,17.19691899999998,17.388582999999983,17.426383999999985,17.633208000000025,17.72530999999998,17.7765,17.925899000000015,17.985956999999985,18.03461299999998,18.035158000000024,18.33455600000002,18.554474000000027,18.819136000000015,19.294691,19.326833000000022,19.370652000000007,19.391025000000013,19.50171900000001,19.568479000000025,19.721015000000023,19.742188999999996,2.0741820000000075,2.190782000000013,2.414432999999974,2.763974000000019,2.792461000000003,2.9027659999999855,2.9076390000000174,2.9481509999999957,2.958406000000025,20.116471999999987,20.465129999999988,21.20068900000001,21.50477699999999,21.81089800000001,22.290697000000023,22.53896800000001,22.783582000000024,23.24664999999999,23.392704999999978,23.70305000000002,24.176241000000005,24.234451999999976,24.29212100000001,25.152215000000012,25.736550000000022,25.955720999999983,26.245765000000006,26.43552199999999,26.738576000000023,26.738576000000023,263.05566699999997,27.455123000000015,27.954201000000012,28.238260000000025,28.66128900000001,3.0292380000000207,3.031385,3.031385,3.104728000000023,3.105985999999973,3.122132000000022,3.1631780000000163,3.1631780000000163,3.1825150000000235,3.1861999999999853,3.1906769999999938,3.2074680000000058,3.2309399999999755,3.2394219999999905,3.248293999999987,3.2692579999999793,3.288909999999987,3.3179549999999836,3.3348550000000046,3.3937690000000202,3.400891999999999,3.427570000000003,3.46416099999999,3.501057000000003,3.5119619999999827,3.5348280000000045,3.535535999999979,3.541954999999973,3.591132000000016,3.595874999999978,3.629410000000007,3.6372420000000147,3.651020000000017,3.674457000000018,3.6869740000000206,3.788045000000011,3.8290949999999953,3.8317579999999793,3.869131999999979,3.8867349999999874,3.8997770000000287,3.9023500000000126,3.970973000000015,4.004128999999978,4.010781000000009,4.013157999999976,4.02364399999999,4.031284000000028,4.034339999999986,4.069560000000024,4.071885000000009,4.07367099999999,4.1598299999999995,4.1598299999999995,4.168802000000028,4.186954000000014,4.259670000000028,4.301670999999999,4.30785800000001,4.326250000000016,4.32803899999999,4.336463999999978,4.396554999999978,4.39680199999998,4.466684999999984,4.5498230000000035,4.551680999999974,4.572103000000027,4.584344999999985,4.592156999999986,4.637402000000009,4.637402000000009,4.645713999999998,4.661412999999982,4.677417999999989,4.681489999999997,4.6883359999999925,4.693658000000028,4.698712999999998,4.703738999999985,4.722540999999978,4.746127999999999,4.786383999999998,4.895690000000002,4.927435000000003,4.949255999999991,4.955774000000019,4.9658640000000105,4.978013999999973,4.9782599999999775,4.982582999999977,5.010145000000023,5.010145000000023,5.027368000000024,5.050145999999984,5.085066999999981,5.089823000000024,5.127470000000017,5.127541000000008,5.157640000000015,5.197153000000014,5.208763999999974,5.216631000000007,5.220619999999997,5.220917999999983,5.241790999999978,5.243516,5.298768999999993,5.302400999999975,5.351732000000027,5.358871000000022,5.395045999999979,5.42818699999998,5.428384999999992,5.440371000000027,5.451302999999996,5.451569000000006,5.453372000000002,5.571251000000018,5.583474000000024,5.603423000000021,5.625193000000024,5.62644499999999,5.6379459999999995,5.639406000000008,5.644623000000024,5.647972999999979,5.654788999999994,5.657871,5.677160000000015,5.684604999999976,5.704746999999998,5.721533000000022,5.754840999999999,5.787960999999996,5.799408000000028,5.836040000000025,5.910754999999995,5.935788000000002,5.98094500000002,5.99198100000001,6.071149999999989,6.167112999999972,6.2217110000000275,6.242680000000007,6.247160000000008,6.299575000000004,6.318251999999973,6.3246240000000284,6.346104000000025,6.390022999999985,6.390474999999981,6.393546000000015,6.406645000000026,6.428521999999987,6.428521999999987,6.4325509999999895,6.474005999999974,6.486009000000024,6.53541899999999,6.588966000000028,6.611136999999985,6.65598,6.661902999999995,6.685046,6.870761000000016,6.897823000000017,6.946781999999985,6.962023999999985,7.0745319999999765,7.079858999999999,7.203704000000016,7.231561999999997,7.396878000000015,7.526357000000019,7.5280500000000075,7.533020000000022,7.540190999999993,7.541165999999976,7.546674999999993,7.567698000000007,7.608597999999972,7.6137899999999945,7.6496609999999805,7.695861999999977,7.754680000000008,7.761375999999984,7.802596999999992,7.803015000000016,7.852875999999981,7.85482300000001,7.881430000000023,7.897682999999972,7.9383980000000065,7.953649999999982,7.977274000000023,7.9786040000000185,8.042038999999988,8.051116999999977,8.058231999999975,8.06355000000002,8.063913000000014,8.065433999999982,8.096699000000001,8.106979000000024,8.11287900000002,8.114483000000007,8.116393000000016,8.123027999999977,8.127981999999975,8.133666000000005,8.133793000000026,8.139561000000015,8.158335000000022,8.16064,8.161336000000006,8.162181999999973,8.162982999999997,8.171137999999985,8.171824000000015,8.176378,8.180379000000016,8.188331000000005,8.19033300000001,8.199669000000029,8.202963000000011,8.205122000000017,8.209947999999997,8.212396000000012,8.214103000000023,8.225754999999992,8.228440999999975,8.236157999999989,8.236670000000004,8.237368000000004,8.246976000000018,8.248426999999992,8.253366000000028,8.257947000000001,8.26767799999999,8.268155999999976,8.272303000000022,8.27318200000002,8.273479000000009,8.275866000000008,8.282681000000025,8.28576099999998,8.287439000000006,8.295105999999976,8.300284999999974,8.301493999999991,8.309056999999996,8.312666999999976,8.313319999999976,8.316741999999977,8.316802999999993,8.319496000000015,8.319905000000006,8.32196399999998,8.33465000000001,8.33758899999998,8.339861999999982,8.343349999999987,8.345591000000013,8.34741600000001,8.351251999999988,8.353306999999973,8.355660999999998,8.35797500000001,8.359123000000011,8.359248999999977,8.361414000000025,8.362278000000003,8.365722000000005,8.367165999999997,8.369223999999974,8.37142399999999,8.372978999999987,8.377184,8.378419000000008,8.380657999999983,8.384374999999977,8.384838000000002,8.38826499999999,8.389519000000007,8.389977999999985,8.391399999999976,8.399541,8.401728999999989,8.40463699999998,8.412535999999989,8.418292000000008,8.418292000000008,8.419540999999981,8.432033999999987,8.436644000000001,8.438633999999979,8.443922999999984,8.445126000000016,8.44520399999999,8.448188000000016,8.454391999999984,8.459992,8.460725000000025,8.471234999999979,8.471527999999978,8.47944799999999,8.481763999999998,8.486855999999989,8.489449999999977,8.48962499999999,8.50250699999998,8.503026999999975,8.505698999999993,8.507151000000022,8.513235000000009,8.521359000000018,8.525622999999996,8.526872000000026,8.529190000000028,8.535190999999998,8.53812499999998,8.539010000000019,8.539371000000017,8.547703000000013,8.54963600000002,8.554725000000019,8.566249000000028,8.56833499999999,8.57478900000001,8.575331000000006,8.59310499999998,8.595698000000027,8.597024999999974,8.603522999999996,8.612051000000008,8.612723000000017,8.621368000000018,8.624873999999977,8.626608999999974,8.631421999999986,8.63256899999999,8.639211999999986,8.647650999999996,8.647655999999984,8.65259900000001,8.666755000000023,8.669451999999978,8.671897999999999,8.673334000000011,8.676539999999989,8.676541999999984,8.686063999999988,8.691044999999974,8.696206000000018,8.706594999999993,8.708612000000016,8.710546000000022,8.713917999999978,8.730766000000017,8.73489699999999,8.737711999999988,8.757464000000027,8.764999999999986,8.809542000000022,8.813491999999997,8.823485000000005,8.834194000000025,8.847305000000006,8.852978000000007,8.855246000000022,8.867098999999996,8.867160000000013,8.871987999999988,8.872235999999987,8.877161999999998,8.884758999999974,8.885359999999991,8.896462999999983,8.938504000000023,8.94348100000002,8.948960999999997,8.988140999999985,9.012393999999972,9.023325,9.029688000000021,9.036518000000001,9.042911000000004,9.042911000000004,9.05015400000002,9.053764999999999,9.077519999999993,9.078524000000016,9.123334,9.125787000000003,9.14443399999999,9.164394000000016,9.177406000000019,9.20016800000002,9.233752999999979,9.236719999999991,9.237805999999978,9.24724500000002,9.256552,9.311277000000018,9.314472000000023,9.324853000000019,9.335095000000024,9.338369,9.34037699999999,9.377050999999994,9.405439999999999,9.414457000000027,9.426608999999985,9.442176000000018,9.445582000000002,9.448505000000011,9.460384999999974,9.460384999999974,9.468001000000015,9.469430999999986,9.469588999999985,9.483652000000006,9.492338000000018,9.507689000000028,9.508412000000021,9.512595999999974,9.51297599999998,9.515115999999978,9.556629999999984,9.560936000000027,9.566130999999984,9.576274000000012,9.582446000000004,9.606822000000022,9.608585000000005,9.615522999999996,9.623440000000016,9.655497000000025,9.685047999999995,9.693380999999988,9.759818999999993,9.788680999999997,9.81468000000001,9.82288699999998,9.829159000000004,9.829363999999998,9.830933000000016,9.835459000000014,9.848674000000017,9.862322000000006,9.877218000000028,9.915767000000017,9.943819000000019,9.944711999999981,9.946091000000024,9.960337999999979,9.976361999999995,9.998279000000025];

output = document.getElementById("output");
output.innerHTML += Math.max(...arr) + '<br />';
output.innerHTML += arr.sort()[arr.length-1] + '<br />';
output.innerHTML += arr.sort()[arr.length-2] + '<br />';
output.innerHTML += arr.sort()[0] + '<br />';

output.innerHTML += '---------------------------------------' + '<br />'

a = [1,2,3,0];
output.innerHTML += Math.max(...a) + '<br />';
output.innerHTML += a.sort()[a.length-1] + '<br />';
output.innerHTML += a.sort()[a.length-2] + '<br />';
output.innerHTML += a.sort()[0] + '<br />';
<html>
  <body>
    <div id='output'></div>
  </body>
</html>

Shouldn't the last element in the sorted array be the max?

4
  • What is the problem? Commented Apr 4, 2019 at 19:30
  • By default sort sorts by strings, use a custom sort function to sort by numbers. Commented Apr 4, 2019 at 19:32
  • @Teemu Not always, only by default if no compareFunction is specified. Commented Apr 4, 2019 at 19:35
  • Another situation when sort() may give unexpected results is if there are array items that cause the callback function to evaluate to NaN. The behavior in that case is undefined, and the V8 script engine, for example, is likely to leave the array in a non-sorted state. See github.com/nodejs/node/issues/53506 for explanations and tips on how to avoid the issue. Commented Mar 9 at 16:34

2 Answers 2

5

If you want to sort numbers you should pass a call back. If you don't pass a callback it will sort() it as strings. According to MDN

The default sort order is built upon converting the elements into strings, then comparing their sequences of UTF-16 code units values.

var array = [1, 10, 100, 20];

array.sort((a, b) => a - b);
console.log(...array);

array.sort();
console.log(...array);

Note:sort() modifies the original array. You are using sort() like you assume it doesnot. In this case it works fine but maybe in other cases it can cause problems

Sign up to request clarification or add additional context in comments.

10 Comments

But typeof returns numbers
@mbojko no, it's not. with missing cb, you get [1, 10, 100, 20]
So the what all browsers are doing is convert all numbers to strings and then sort them? Brilliant!
@SaravanabalagiRamachandran See i have added a documentation. It compares on the basis of utf-16 codes by default
@SaravanabalagiRamachandran I didn't created javascript.
|
0

Math.max called this way will be unreliable. From MDN:

However, both spread (...) and apply will either fail or return the wrong result if the array has too many elements, because they try to pass the array elements as function parameters. See Using apply and built-in functions for more details. The reduce solution does not have this problem.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/max

1 Comment

There are values bigger than 9.998279000000025 (The value reported by .sort()) in the array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.