A.sort(null) IE and non-IE

Discussion in 'Javascript' started by ddailey, Jan 3, 2008.

  1. ddailey

    ddailey Guest

    For reasons that are rather complicated, I have provided my JavaScript
    sort() method of an array with the null parameter (rather than an
    empty parameter list).

    A=[2,3,8,4,1,0]
    alert(A.sort(null))

    In Opera, FF and Safari, A.sort(null) returns the same array as A, but
    IE actually performs the default ascii-order sort. I would assume the
    majority of browsers here are following the ECMA-script standard. Is
    IE's behavior here a bug?
    ddailey, Jan 3, 2008
    #1
    1. Advertising

  2. ddailey wrote:
    > A=[2,3,8,4,1,0]
    > alert(A.sort(null))
    >
    > In Opera, FF and Safari, A.sort(null) returns the same array as A, but
    > IE actually performs the default ascii-order sort. I would assume the
    > majority of browsers here are following the ECMA-script standard. Is
    > IE's behavior here a bug?


    It isn't:

    | If comparefn is not undefined and is not a consistent comparison
    | function for the elements of this array (see below), the behaviour
    | of sort is implementation-defined. [...]

    Simply don't pass `null' as argument:

    | 15.4.4.11 Array.prototype.sort (comparefn)
    |
    | The elements of this array are sorted. The sort is not necessarily stable
    | (that is, elements that compare equal do not necessarily remain in their
    | original order). If comparefn is not undefined, it should be a function
    | that accepts two arguments x and y and returns a negative value if x < y,
    | zero if x = y, or a positive value if x > y.


    PointedEars
    Thomas 'PointedEars' Lahn, Jan 3, 2008
    #2
    1. Advertising

  3. Thomas 'PointedEars' Lahn wrote:
    > ddailey wrote:
    >> A=[2,3,8,4,1,0]
    >> alert(A.sort(null))
    >>
    >> In Opera, FF and Safari, A.sort(null) returns the same array as A, but
    >> IE actually performs the default ascii-order sort. I would assume the
    >> majority of browsers here are following the ECMA-script standard. Is


    _ECMAScript_

    >> IE's behavior here a bug?

    >
    > It isn't:
    > [...]


    Besides, you have not tested carefully enough.

    Test case
    ----------

    var a = [2, 3, 8, 4, 1, 0];
    window.alert(a.sort(null));
    window.alert(a);

    Results
    --------

    Opera/9.24 (Windows NT 5.1; U; en):

    0,1,2,3,4,8
    0,1,2,3,4,8

    Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.11) Gecko/20071127
    Firefox/2.0.0.11:

    Error: TypeError: invalid Array.prototype.sort argument
    Code: window.alert(a.sort(null));
    Line: 2

    (no message window)

    Mozilla/5.0 (Windows; U; Windows NT 5.1; de-DE) AppleWebKit/523.15 (KHTML,
    like Gecko) Version/3.0 Safari/523.15:

    Error: TypeError: Null value
    Line: 2

    (no message window)


    PointedEars
    --
    Prototype.js was written by people who don't know javascript for people
    who don't know javascript. People who don't know javascript are not
    the best source of advice on designing systems that use javascript.
    -- Richard Cornford, cljs, <f806at$ail$1$>
    Thomas 'PointedEars' Lahn, Jan 3, 2008
    #3
  4. In comp.lang.javascript message <f735447e-98a7-4235-b710-fa8cb17cec56@m3
    4g2000hsf.googlegroups.com>, Thu, 3 Jan 2008 10:27:20, ddailey
    <> posted:
    >For reasons that are rather complicated, I have provided my JavaScript
    >sort() method of an array with the null parameter (rather than an
    >empty parameter list).
    >
    >A=[2,3,8,4,1,0]
    >alert(A.sort(null))
    >
    >In Opera, FF and Safari, A.sort(null) returns the same array as A, but
    >IE actually performs the default ascii-order sort. I would assume the
    >majority of browsers here are following the ECMA-script standard. Is
    >IE's behavior here a bug?



    The standard is unclear. One cannot tell whether, by 'undefined', it
    means 'not-defined' or it means 'the value that U has after var U ;' .
    It's also not clear to me whether the standard says what should result
    if no argument, or an argument like U, is given.

    If you've not done so, it might be worth trying the use of U rather than
    null.

    --
    (c) John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v6.05 MIME.
    Web <URL:http://www.merlyn.demon.co.uk/> - FAQqish topics, acronyms & links;
    Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
    No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.
    Dr J R Stockton, Jan 4, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. vizlab
    Replies:
    3
    Views:
    4,184
    Michael Bar-Sinai
    Oct 17, 2007
  2. Replies:
    5
    Views:
    26,527
    Mike Schilling
    Mar 29, 2006
  3. Col
    Replies:
    1
    Views:
    612
    peter koch
    Apr 21, 2006
  4. Francois Grieu
    Replies:
    14
    Views:
    791
    Micah Cowan
    Mar 7, 2008
  5. Navin
    Replies:
    1
    Views:
    675
    Ken Schaefer
    Sep 9, 2003
Loading...

Share This Page