I'm wondering WHY when I sort items in the original array they show up in a weird order when processing but the result is correct. Here's the code var arr = [100, 8, 20, 12];  var x = 1;  console.log(&quot;Original Array = &quot; + arr) arr.sort(function(a,b){      console.log(&quot;Set &quot; + x++) //for tracking purposes     console.log(a, b)     console.log(&quot;a-b = &quot; + (a-b))     return a-b; })  console.log(arr)  I've tried this on both SoloLearn and on Chrome to see if it had something to do with the browser itself but I get the same results RESULT:  I want to understand why this behavior of sorting happens (a=8 and b=100...etc) Any help or reference would be greatly appreciated.
82376
Which algorithm does the JavaScript Array#sort() function use?  I understand that it can take all manner of arguments and functions to perform different kinds of sorts, I'm simply interested in which algorithm the vanilla sort uses.
90875
How would I go about reordering divs without altering the HTML source code?  example, I want divs to appear in order #div2, #div1, #div3, but in the HTML they are:  &lt;div id="#div1"&gt;&lt;/div&gt;   &lt;div id="#div2"&gt;&lt;/div&gt;   &lt;div id="#div3"&gt;&lt;/div&gt;   Thanks!
89104
Is the sorting algorithm used by .NET's Array.Sort() method a  algorithm?
95963
Let's say that I have two arrays (in Java),  int[] numbers; and int[] colors;  Each ith element of numbers corresponds to its ith element in colors. Ex, numbers = {4,2,1}      colors = {0x11, 0x24, 0x01}; Means that number 4 is color 0x11, number 2 is 0x24, etc.  I want to sort the numbers array, but then still have it so each element matches up with its pair in colors.  Ex. numbers = {1,2,4};     colors = {0x01,0x24,0x11};  What's the cleanest, simplest way to do this? The arrays have a few thousand items, so being in place would be best, but not required. Would it make sense to do an Arrays.sort() and a custom comparator? Using library functions as much as possible is preferable.  Note: I know the "best" solution is to make a class for the two elements and use a custom comparator. This question is meant to ask people for the quickest way to code this. Imagine being at a programming competition, you wouldn't want to be making all these extra classes, anonymous classes for the comparator, etc. Better yet, forget Java; how would you code it in C?
23573
Consider the following input to sort:  cat &gt; foo &lt;&lt;EOM D,,5014978 DD,,25 D,I,1972765530 D,Y,4223624 -,Y,71285059 YA,I,2 EOM   Now try running sort foo  The output is not sorted when trying this on any of my linux boxes (gnu coreutils versions 6.9-7.4).  The output is sorted when run under cygwin (gnu coretuils 8.5).  Comments?
3286
Does the order in which I set properties using the object initializer syntax get executed in the exact same order?  For instance if I do this:  var s = new Person { FirstName = "Micah",                      LastName = "Martin",                      IsLoaded = true                    }   will each property get set in the same order?
81019
It seems that sort behaves strangely on lines such as &gt;&gt;b    $ cat test a &gt;&gt;b b c &gt; &gt;&gt;  $ sort test &gt; &gt;&gt; a b &gt;&gt;b c   I would expect the &gt;&gt;b line to be the third line output by sort but it is the fifth. Why does it do this and is there a way to make sort give my expected output?    I'm using GNU/Linux Ubuntu 16.04.
18904
I believe there's a way to find the kth largest element in an unsorted array of length n in O(n).  Or perhaps it's "expected" O(n) or something.  How can we do this?
123805
After reading through a few other questions I was just asking myself: How was the Order of Operations defined, and why is it this specific order and not a different one?  Most of us know things like multiplication/division before addition/subtraction, parentheses first, etc - but what's the actual reason behind it? I'm probably biased by following those rules since childhood, so I can't really think of any other way.  2 + 2 x 2 = 6 and not 8  But if the order would be changed, let's say to "addition/subtraction before multiplication/division" would that order still work if we assume that mathematics would build up on it? Or is there some strange mathematical problem if we would be using a different order?  Of course, parentheses have a grouping function so they should always come first - I'm mainly talking about the exponents, multiplication/division, addition/subtraction (and maybe other operations left out).
38365
How can I left-align the numbers in an ordered list?  1.  an item // skip some items for brevity  9.  another item 10. notice the 1 is under the 9, and the item contents also line up   Change the character after the number in an ordered list?  1) an item   Also is there a CSS solution to change from numbers to alphabetic/roman lists instead of using the type attribute on the ol element.  I am mostly interested in answers that work on Firefox 3.
45508
I am aware that it is possible to sort primarily by one field and secondarily by another field. For example:   $ echo -e "1 5\n1 4\n8 2\n1 9" | sort -k 1,1n -k 2,2n 1 4 1 5 1 9 8 2 $    However, am I correct that sort always keeps the lines intact, e.g. it is not possible to sort in a way that output would be following:  1 2 1 4 1 5 8 9   If this is not possible with sort, then what would be the correct tool for this?
75403
I have an array:  data.Dealer.car[0] data.Dealer.car[1] data.Dealer.car[2]   If I do this:  alert(data.Dealer.car.length); delete data.Dealer.car[1]; alert(data.Dealer.car.length);   It gives me the same count each time. Does the removed element still exist?
17596
Using the same sort command with the same input produces different results on different machines.  How do I fix that?
93651
I want to compare 2 arrays of objects in JavaScript code.  The objects have 8 total properties, but each object will not have a value for each, and the arrays are never going to be any larger than 8 items each, so maybe the brute force method of traversing each and then looking at the values of the 8 properties is the easiest way to do what I want to do, but before implementing, I wanted to see if anyone had a more elegant solution. Any thoughts?
58697
The alignment of items in the block Questions that may already have your answer is weird. The first question score is shown before the question and subsequent question and scores doesn't match. Here is a screenshot:     Right now, I'm using Chrome Version 28.0.1500.95 m, but it looks the same on Internet Explorer. Reproduced on Firefox 22.0  Edit: Stack Overflow looks normal.
87065
I'm looking for some sample code that will sort the list items in an HTML list by alphabetical order.  Can anyone help?  Here is a sample list for people to work with:  &lt;ul class="alphaList"&gt;     &lt;li&gt;apples&lt;/li&gt;     &lt;li&gt;cats&lt;/li&gt;     &lt;li&gt;bears&lt;/li&gt; &lt;/ul&gt;
92960
As can be seen in the Mozilla changlog for JavaScript 1.7 they have added destructuring assignment. Sadly I'm not very fond of the syntax (why write a and b twice?):  var a, b;   [a, b] = f();   Something like this would have been a lot better:  var [a, b] = f();   That would still be backwards compatible. Python-like destructuring would not be backwards compatible.  Anyway the best solution for JavaScript 1.5 that I have been able to come up with is:  function assign(array, map) {     var o = Object();     var i = 0;     $.each(map, function(e, _) {         o[e] = array[i++];     });     return o; }   Which works like:  var array = [1,2]; var _ = assign[array, { var1: null, var2: null }); _.var1; // prints 1 _.var2; // prints 2   But this really sucks because _ has no meaning. It's just an empty shell to store the names. But sadly it's needed because JavaScript doesn't have pointers. On the plus side you can assign default values in the case the values are not matched. Also note that this solution doesn't try to slice the array. So you can't do something like {first: 0, rest: 0}. But that could easily be done, if one wanted that behavior.  What is a better solution?