Random sorting of js array

js randomly sorts an array. How is it performed in sort?

var arr = [1,2,3,4,5,6,7,8,9,10];
      arr.sort(function(){
          return Math.random() - 0.5;
      })
      console.log(arr);
Mar.20,2021

Array.prototype.sort only defines the behavior in the es specification, and does not specify the implementation of the specific sorting algorithm, so the implementation of each engine is different.

for V8, insert sort is used if the array length is less than 10, otherwise quick sort
https://github.com/v8/v8/blob.

is used.

clipboard.png

4V8array.sorttypedarray.sorttorqueV8cPPDSL

array.sortcommitCPP

clipboard.png


The boss upstairs explained how it works in sort . Let me answer "js randomly sorts an array".

the method of the subject is wrong, and almost all elements (at least in Chrome) will stay in their original place!

< table id= "results" > < tbody > < tr > < th > Results < / th > < th > slot 0 < / th > < th > slot 1 < / th > < th > slot 2 < / th > < th > slot 3 < / th > < th > slot 4 < / th > < th > slot 5 < / th > < th > slot 6 < / th > < th > slot 7 < / th > < th > slot 8 < / th > < th > slot 9 < / th > < / tr > < tr > < th > ads < / th > < td class= "strongbias" > 2919 < / td > < td > 2889 < / td > < td > 1924 < / td > < td > 1120 < / td > < td > 581 < / td > < td > 296 < / td > < td > 141 < / td > < td > 83 < / td > < td > 34 < / td > < td > 13 < / td > < / tr > < tr > < th > bones < / th > < td > 2925 < / td > < td class= "strongbias" > 2859 < / td > < td > 1901 < / td > < td > 1104 < / td > < td > 604 < / td > < td > 294 < / td > < td > 165 < / td > < td > 77 < / td > < td > 48 < / td > < td > 23 < / td > < / tr > < tr > < th > clips < / th > < td > 1883 < / td > < td > 1951 < / td > < td class= "bias" > 2259 < / td > < td > 1689 < / td > < td > 1030 < / td > < td > 585 < / td > < td > 324 < / td > < td > 165 < / td > < td > 78 < / td > < td > 36 < / td > < / tr > < tr > < th > dudes < / th > < td > 1096 < / td > < td > 1081 < / td > < td > 1742 < / td > < td class= "bias" > 2228 < / td > < td > 1734 < / td > < td > 1005 < / td > < td > 592 < / td > < td > 310 < / td > < td > 144 < / td > < td > 68 < / td > < / tr > < tr > < th > eBay < / th > < td > 550 < / td > < td > 603 < / td > < td > 1059 < / td > < td > 1760 < / td > < td class= "bias" > 2229 < / td > < td > 1785 < / td > < td > 1034 < / td > < td > 564 < / td > < td > 274 < / td > < td > 142 < / td > < / tr > < tr > < th > fences < / th > < td > 306 < / td > < td > 298 < / td > < td > 558 < / td > < td > 993 < / td > < td > 1738 < / td > < td class= "bias" > 2319 < / td > < td > 1817 < / td > < td > 1064 < / td > < td > 608 < / td > < td > 299 < / td > < / tr > < tr > < th > gags < / th > < td > 161 < / td > < td > 172 < / td > < td > 295 < / td > < td > 569 < / td > < td > 1056 < / td > < td > 1718 < / td > < td class= "bias" > 2346 < / td > < td > 1915 < / td > < td > 1131 < / td > < td > 637 < / td > < / tr > < tr > < th > hats < / th > < td > 89 < / td > < td > 80 < / td > < td > 147 < / td > < td > 323 < / td > < td > 575 < / td > < td > 1127 < / td > < td > 1731 < / td > < td class= "bias" > 2595 < / td > < td > 2068 < / td > < td > 1265 < / td > < / tr > < tr > < th > iTunes < / th > < td > 45 < / td > < td > 45 < / td > < td > 82 < / td > < td > 146 < / td > < td > 286 < / td > < td > 564 < / td > < td > 1234 < / td > < td > 2015 < / td > < td class= "strongbias" > 3144 < / td > < td > 2439 < / td > < / tr > < tr > < th > jacks < / th > < td > 26 < / td > < td > 22 < / td > < td > 33 < / td > < td > 68 < / td > < td > 167 < / td > < td > 307 < / td > < td > 616 < / td > < td > 1212 < / td > < td > 2471 < / td > < td class= "strongbias" > 5078 < / td > < / tr > < / tbody > < / table >

https://jsfiddle.net/rcmp0aLL/

in fact, whether the subject's method can disrupt the array or not is dependent on the concrete implementation of sort . The specific implementation of Chrome please see the boss upstairs, in any case, the result is not good.

what is the correct posture?

with lodash _ .shuffle , it is simple, fast, direct and easy to maintain.

or, implement a Fisher-Yates algorithm:

  https://stackoverflow.com/que.

MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3c285-2c2fa.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3c285-2c2fa.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?