array sorting with blank spaces: do IE and Mozilla handle this differently?

Discussion in 'Javascript' started by John Bullock, Jul 8, 2004.

  1. John Bullock

    John Bullock Guest

    Hello,

    I am at wit's end with an array sorting problem. I have a
    simple table-sorting function which must, at times, sort on columns that
    include entries with nothing but a space (@nbsp;). I want all of the
    spaces to be put in the first slots of the array. IE 6 does this. But
    Firefox 0.9.1 doesn't, and I don't know why.
    I have not been able to reproduce it in very simple form (which
    is itself a puzzle). But example code is available at
    http://cgi.stanford.edu/~bullock2/dirlist.pl?threat: at this page,
    sorting on the "Size" column works as I want in IE, but not in Firefox.
    I suspect that the problem lies with my code and not with the browsers.
    For what it's worth, here is the sorting function:

    function RowCompareNumbers(a, b) {
    if (a.value==" " & b.value==" ") return 0;
    else if (a.value==" ") return -1;
    else if (b.value==" ") return 1;
    else {
    var aVal = parseInt(a.value);
    var bVal = parseInt(b.value);
    return (aVal - bVal);
    }
    }

    (I know this can be made much more concise, but for the moment,
    I'm not worried about it.)

    There is also getInnerText() function which retrieves the text
    from the table cells; it may be the culprit.

    Many thanks,
    --John
     
    John Bullock, Jul 8, 2004
    #1
    1. Advertising

  2. John Bullock

    John Bullock Guest

    By the way, there is a long and useful thread from mid-February on
    browser discrepancies in array sorting, but I couldn't see that it
    helped for this particular problem.


    "John Bullock" <> wrote in message
    news:ccjfu3$atr$...
    >
    > Hello,
    >
    > I am at wit's end with an array sorting problem. I have a
    > simple table-sorting function which must, at times, sort on columns

    that
    > include entries with nothing but a space (@nbsp;). I want all of the
    > spaces to be put in the first slots of the array. IE 6 does this.

    But
    > Firefox 0.9.1 doesn't, and I don't know why.
    > I have not been able to reproduce it in very simple form

    (which
    > is itself a puzzle). But example code is available at
    > http://cgi.stanford.edu/~bullock2/dirlist.pl?threat: at this page,
    > sorting on the "Size" column works as I want in IE, but not in

    Firefox.
    > I suspect that the problem lies with my code and not with the

    browsers.
    > For what it's worth, here is the sorting function:
    >
    > function RowCompareNumbers(a, b) {
    > if (a.value==" " & b.value==" ") return 0;
    > else if (a.value==" ") return -1;
    > else if (b.value==" ") return 1;
    > else {
    > var aVal = parseInt(a.value);
    > var bVal = parseInt(b.value);
    > return (aVal - bVal);
    > }
    > }
    >
    > (I know this can be made much more concise, but for the

    moment,
    > I'm not worried about it.)
    >
    > There is also getInnerText() function which retrieves the text
    > from the table cells; it may be the culprit.
    >
    > Many thanks,
    > --John
    >
    >
     
    John Bullock, Jul 8, 2004
    #2
    1. Advertising

  3. John Bullock

    John Bullock Guest

    I found an array sorting function that produced identical (and
    desirable) behavior in both IE 6 and Firefox 0.9.1. The question
    becomes: why do the two functions produce different behavior in FireFox
    but not in IE?

    The function that works for both browsers:

    function RowCompareNumbersX(a, b) {
    var aVal = parseInt(a.value);
    var bVal = parseInt(b.value);
    if (isNaN(aVal) && isNaN(bVal)) return 0;
    else if (isNaN(parseInt(a.value))) return -1;
    else if (isNaN(parseInt(b.value))) return 1;
    else return (aVal - bVal);
    }

    The function that produces a different sort (from the one above) in
    Firefox, but the same in IE:

    function RowCompareNumbers(a, b) {
    var aVal = parseInt(a.value);
    var bVal = parseInt(b.value);
    if (a.value==" " & b.value==" ") return 0;
    else if (a.value==" ") return -1;
    else if (b.value==" ") return 1;
    else return (aVal - bVal);
    }

    Thanks again,
    --John



    "John Bullock" <> wrote in message
    news:ccjfu3$atr$...
    >
    > Hello,
    >
    > I am at wit's end with an array sorting problem. I have a
    > simple table-sorting function which must, at times, sort on columns

    that
    > include entries with nothing but a space (@nbsp;). I want all of the
    > spaces to be put in the first slots of the array. IE 6 does this.

    But
    > Firefox 0.9.1 doesn't, and I don't know why.
    > I have not been able to reproduce it in very simple form

    (which
    > is itself a puzzle). But example code is available at
    > http://cgi.stanford.edu/~bullock2/dirlist.pl?threat: at this page,
    > sorting on the "Size" column works as I want in IE, but not in

    Firefox.
    > I suspect that the problem lies with my code and not with the

    browsers.
    > For what it's worth, here is the sorting function:
    >
    > function RowCompareNumbers(a, b) {
    > if (a.value==" " & b.value==" ") return 0;
    > else if (a.value==" ") return -1;
    > else if (b.value==" ") return 1;
    > else {
    > var aVal = parseInt(a.value);
    > var bVal = parseInt(b.value);
    > return (aVal - bVal);
    > }
    > }
    >
    > (I know this can be made much more concise, but for the

    moment,
    > I'm not worried about it.)
    >
    > There is also getInnerText() function which retrieves the text
    > from the table cells; it may be the culprit.
    >
    > Many thanks,
    > --John
    >
    >
     
    John Bullock, Jul 8, 2004
    #3
  4. Re: array sorting with blank spaces: do IE and Mozilla handle thisdifferently?

    "John Bullock" <> writes:

    > The question becomes: why do the two functions produce different
    > behavior in FireFox but not in IE?


    > The function that produces a different sort (from the one above) in
    > Firefox, but the same in IE:
    >
    > function RowCompareNumbers(a, b) {
    > var aVal = parseInt(a.value);


    This should be
    var aVal = parseInt(a.value, 10);

    The original version (without the base as argument to parseInt) will
    read "012" as 10 in some browsers (read as octal because of initial
    0).

    > var bVal = parseInt(b.value);
    > if (a.value==" " & b.value==" ") return 0;

    ^
    ^ that is bitwise and.

    You compare the valus to strings of length 1 contatining only one
    space. I'll habe to assume that you know that the value won't be the
    empty string or two spaces or something else. However, my guess is
    that this is the problem you are having.

    Bitwise and turns both arguments into 32 bit integers and then
    performs the bitwise and on these. In this case, it shouldn't matter,
    since booleans turned into numbers become 0 and 1 (for false and true)
    and bitwise and gives the same result as logical and.

    /L
    --
    Lasse Reichstein Nielsen -
    DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
    'Faith without judgement merely degrades the spirit divine.'
     
    Lasse Reichstein Nielsen, Jul 8, 2004
    #4
  5. John Bullock

    Grant Wagner Guest

    Re: array sorting with blank spaces: do IE and Mozilla handle thisdifferently?

    John Bullock wrote:

    > Hello,
    >
    > I am at wit's end with an array sorting problem. I have a
    > simple table-sorting function which must, at times, sort on columns that
    > include entries with nothing but a space (@nbsp;). I want all of the
    > spaces to be put in the first slots of the array. IE 6 does this. But
    > Firefox 0.9.1 doesn't, and I don't know why.
    > I have not been able to reproduce it in very simple form (which
    > is itself a puzzle). But example code is available at
    > http://cgi.stanford.edu/~bullock2/dirlist.pl?threat: at this page,
    > sorting on the "Size" column works as I want in IE, but not in Firefox.
    > I suspect that the problem lies with my code and not with the browsers.
    > For what it's worth, here is the sorting function:
    >
    > function RowCompareNumbers(a, b) {
    > if (a.value==" " & b.value==" ") return 0;


    You're using a bitwise & here, not a logical one. Although it may be working
    as expected most of the time, I'm sure it's not what you intended.

    > else if (a.value==" ") return -1;
    > else if (b.value==" ") return 1;
    > else {
    > var aVal = parseInt(a.value);
    > var bVal = parseInt(b.value);
    > return (aVal - bVal);
    > }
    > }


    Your comparator is doing way more work then is necessary. You just need to
    normalize any non-numeric data to numbers, in this case, zero (because you
    want them first in the array). The way to achieve this is by converting each
    value to a number, then changing anything that isNaN into 0. Here is a
    sample that works in IE6SP1, Netscape 4.78, Opera 7.5.2 and Firefox 0.9.1:

    // sample data
    var myArray = [
    {value:123},
    {value:'&nbsp;'},
    {value:234},
    {value:' '},
    {value:'&nbsp;'},
    {value:345},
    {value:' '}
    ];
    // sort using custom comparator
    myArray.sort(RowCompareNumbers);

    // test output
    for (var i = 0; i < myArray.length; i++) {
    document.write('[' + myArray.value + ']<br>');
    }

    // comparator
    function RowCompareNumbers(a, b) {
    var aa = +a.value || 0;
    var bb = +b.value || 0;
    return (aa - bb);
    }

    > (I know this can be made much more concise, but for the moment,
    > I'm not worried about it.)


    Cleaning up the comparator to generate reliable, consistent sorted lists
    with sample data is the first step in getting this working, so you should be
    worried about it. You're doing parseInt() without a second parameter, so if
    any of your data contains leading zeros, it'll be converted to hexadecimal,
    which might contain letters, which would cause an error when you attempt to
    "return (aVal - bVal);".

    > There is also getInnerText() function which retrieves the text
    > from the table cells; it may be the culprit.


    Well then, what I'd suggest you do is populate an array with the data you
    are getting from the table cells to see what that is exactly (by dumping it
    to a <textarea> or alert()ing it for example.

    Also, Mozilla doesn't support the "innerText" property, so I'm hoping your
    "getInnerText()" function actually uses "innerHTML" (although it should be
    using nodeValue). Even if it does use "innerHTML", the representation of
    "innerHTML" differs from browser to browser (see <url:
    http://jibbering.com/faq/faq_notes/alt_dynwrite.html#innHTest />) so what
    IE is retrieving from the table cell might be completely different from what
    Mozilla is retrieving.

    Check the data you are trying to sort before checking anything else
    (although you can use my comparator). Garbage in, garbage out as they say.

    --
    Grant Wagner <>
    comp.lang.javascript FAQ - http://jibbering.com/faq
     
    Grant Wagner, Jul 8, 2004
    #5
    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. tshad
    Replies:
    13
    Views:
    933
    Mark Parnell
    Mar 23, 2005
  2. beckj
    Replies:
    2
    Views:
    497
    =?ISO-8859-1?Q?G=E9rard_Talbot?=
    Mar 5, 2006
  3. bienwell
    Replies:
    2
    Views:
    833
    bienwell
    Oct 23, 2007
  4. tshad
    Replies:
    1
    Views:
    123
  5. Aziz
    Replies:
    5
    Views:
    151
Loading...

Share This Page