JavaScript array.sort does not work in Chrome
NickName:Pedro Loureiro Ask DateTime:2016-02-26T02:23:47

JavaScript array.sort does not work in Chrome

I'm using the built-in sort() method on an array of objects in JavaScript by providing a compare function which I think meets the needed requirements, as it returns 1, -1 or 0 whether item A compares bigger, smaller or equal to item B, respectively.

This is the code:

aData.sort(function(itemA, itemB) {
    if (itemA.field1 > itemB.field1) return 1;
    if (itemA.field1 < itemB.field1) return -1;
    return 0;
});

This works perfectly in Iceweasel/Firefox (Version 38.6.1) on a Debian 8 VM but it's completely useless when using Chrome (Version 48.0.2564.109 (64-bit)). No sorting whatsoever is done, the array is left completely unchanged!

I've upgraded Chrome, spent several hours searching for solutions for this problem so far but no avail.

Any ideas, guys?

Much obliged!

Copyright Notice:Content Author:「Pedro Loureiro」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/35635270/javascript-array-sort-does-not-work-in-chrome

More about “JavaScript array.sort does not work in Chrome” related questions

JavaScript array.sort does not work in Chrome

I'm using the built-in sort() method on an array of objects in JavaScript by providing a compare function which I think meets the needed requirements, as it returns 1, -1 or 0 whether item A compares

Show Detail

How does sort function work in JavaScript, along with compare function

As already asked: how does sort function work in JavaScript, along with the compare function? If I have an array, and I do array.sort(compare) now it was written in the book that if the compare fun...

Show Detail

XMLHttpRequest does not work in a Chrome app

I am new to XMLHttpRequest and JavaScript and all this stuff. I made a gopro WiFi app to control gopro cameras with XMLHttpRequest, but it does not work with the packaged Chrome app, it works with ...

Show Detail

Javascript does not seem to work in chrome

I'm having a tough time enabling javascript in chrome on my own webpage. Other websites seem to be able to do it pretty easily. Here's my prtty simple code but the start() function just wouldn't wo...

Show Detail

javascript array.sort with undefined values

How does Array.prototype.sort handle undefined values in an array? var array = [1,undefined,2,undefined,3,undefined,4]; var array2 = []; array2[0] = 1;array2[2] = 2;array2[4] = 3;array2[6] = 4; W...

Show Detail

Javascript does not work in Chrome extensions

Inside chrome-extension://&lt;EXTENSION_ID&gt;/hello.html, the following code: &lt;script&gt; alert("hi"); &lt;/script&gt; JS does not work? How to make it work?

Show Detail

Why does this javascript work on firefox but not in Chrome?

This is a javascript for a virtual keyboard. Click here for the source. To use this keyboard all i have to do is use a CSS class in the TextBox control. Here is the TextBox. &lt;asp:TextBox ID="t...

Show Detail

How can the compareTo method work together with array.sort to sort an array?

I know that compareTo can return positive negative and zero values. So if I use public class x implements Comparable &lt;Object O&gt; {/*...*/} and assign inside public int compareTo(int i) { ...

Show Detail

javascript bookmarklet does not work in new tab in chrome

I have this javascript bookmarklet This appends the prompted value to www.oracle.com and opens that url in tab. javascript:window.location.href='http://www.oracle.com/'+prompt("Please enter your ...

Show Detail

How do I sort combining 2 array in javascript (like c# (Array.sort() ))

I have 2 arrays. Names in first array and indexes in second array. I ordered with C# (Array.sort() property). Example : my array name is OrderedNames. var names = OrderedName.SelectMany(x =&gt; x...

Show Detail