6

I'am a javascript newbie, here is code from ExtJS which confuses me:

supportsSort = (function() {
    var a = [1,2,3,4,5].sort(function(){ return 0; });
    return a[0] === 1 && a[1] === 2 && a[2] === 3 && a[3] === 4 && a[4] === 5;
}()),

Is someone can tell me why ExtJS want to do this test?

It is better to attach some examples code.

tshepang
  • 12,111
  • 21
  • 91
  • 136
hsy0
  • 139
  • 1
  • 9
  • I'm up-voting this for actually digging into the framework you are using :) – Emissary Sep 14 '13 at 18:54
  • @Emissary thanks for your replay and up-voting, i agree with your sent "leaving a test for actual support more or less redundant", so i ask the same question on ExtJS forum [link](http://www.sencha.com/forum/showthread.php?271928-Why-does-Ext-want-to-test-browser-supports-sorting) – hsy0 Sep 15 '13 at 02:43
  • @Emissary ha, a guy from ExtJS dev-team has gave an answer, you can see it just follow the link i sent. – hsy0 Sep 15 '13 at 06:43
  • *"Some browsers don't implement a stable sort correctly"* - then I am correct... you have awarded the best answer to the wrong answer :/ – Emissary Sep 15 '13 at 08:54

2 Answers2

2

Hesitant to post this as the answer as I'm admittedly just taking an educated guess but according to MDN, the browser-compatibility for Array.sort is listed as ECMAScript5 and "yes" for everything (as opposed to listing actual version numbers) - leaving a test for actual support more or less redundant.

The variable name is probably a little bit miss-leading though because if you actually follow what it's doing, the function that is passed to sort is just returning 0; typically you might return 1 or -1 depending on your comparison conditions in order to manipulate the order of the array - so by doing this the expected result is that the order of the array remains unchanged.

The return statement is just a chain of boolean checks as to whether the array is still in the same order as it was initially. Arguably then this supportsSort flag is there to check whether or not the browser/Javascript's implementation of the sort function is in fact a stable algorithm.

Community
  • 1
  • 1
Emissary
  • 9,954
  • 8
  • 54
  • 65
  • 1
    i'd asked a same question on ExtJS forum and a guy from their dev-team had gave me a answer, see this [post](http://www.sencha.com/forum/showthread.php?271928-Why-does-Ext-want-to-test-browser-supports-sorting) – hsy0 Sep 15 '13 at 06:46
  • +1 Evan on the Sencha forum indicates that this is actually the correct answer... – rixo Sep 15 '13 at 10:18
  • I'am a so shilly-shally man, i made @Riku as the correct answer because of there are two links in the answer can help me better, but you guys were all thought this is the best answer :(, but still thank you very much, you guys are very helpful! – hsy0 Sep 16 '13 at 01:04
  • @hsiaosiyuan no worries mate. – Emissary Sep 16 '13 at 07:14
0

I think this test is needed to check if browser supports this feature or not.

As this is discussed in this post or in this link you can find Array.prototype.sort([comparator]). There is shown that not all browsers versions supports sort function.

These days there isn't browser which does not support this function (i'm talking about latests versions). But in case if Ext Js is used to develop for earlier versions it becomes needed.

Community
  • 1
  • 1
kuldarim
  • 1,096
  • 8
  • 21
  • 44