/    Sign up×
Community /Pin to ProfileBookmark

Sorting a 2-dim array, firefox screws up, IE is fine…

I have a 2 dimensional array that the user can sort. Each column needs to be sortable. I have written this and in IE it works perfectly and behaves as expected. However Firefox shows very strange behavior.

In this example I have a very simple two dimensional array: column one is a name, column two is a department code. When we sort first on the name, and then on the dept-code, we expect to see the department code sorted, but per department that the names are stil in alphabetical order. Again this works fin on IE, but not in Firefox.

You can see firefox screwing up by repeatedly clicking on “Sort By Dept” because the names end up in a different order every time. (The association between columns never get lost thankfully)

Does anyone understand why Firefox is doing this, and is there a simple work around? It’s not often thet IE does something right … πŸ™‚

I have attached a textfile with a code example, or you can check out [url]https://webspace.utexas.edu/torvald/public/sortExample.html[/url] to see the problem in action.

Thanks!

T

[upl-file uuid=d57d5105-58d9-4658-9c9e-72fd8e0214bb size=2kB]sortExample.txt[/upl-file]

to post a comment
JavaScript

6 Comments(s) ↴

Copy linkTweet thisAlerts:
@jasonahouleJan 31.2008 β€”Β It worked fine for me. I am using FF 2.0.0.11
Copy linkTweet thisAlerts:
@tvhaustinauthorJan 31.2008 β€”Β I use the same FF version. Lemme be even clearer :-)

The original table displays the data as follows:

TRIMBLE, JOHN R E

BARNES, WARNER J S/P

GALLE, OMER R SOC

MCDONALD, STEPHEN L ECO

BROWNING, GRAYSON D PHL

HAIRSTON, MAXINE C E

HAIRSTON, MAXINE C E

VENTO, ARNOLD C S/P

KAUFMANN, RALPH J E

JANNUZI, F TOMASSON ECO

After one click on the sort by dept we see:

KAUFMANN, RALPH J E

HAIRSTON, MAXINE C E

HAIRSTON, MAXINE C E

TRIMBLE, JOHN R E

MCDONALD, STEPHEN L ECO

JANNUZI, F TOMASSON ECO

BROWNING, GRAYSON D PHL

BARNES, WARNER J S/P

VENTO, ARNOLD C S/P

GALLE, OMER R SOC

This is correct: the second column is sorted. But now if we click again, we expect the table to [U]remain the same.[/U] However, FF puts the items in the first column in a different order (the second columns remains correct):

HAIRSTON, MAXINE C E

HAIRSTON, MAXINE C E

TRIMBLE, JOHN R E

KAUFMANN, RALPH J E

JANNUZI, F TOMASSON ECO

MCDONALD, STEPHEN L ECO

BROWNING, GRAYSON D PHL

VENTO, ARNOLD C S/P

BARNES, WARNER J S/P

GALLE, OMER R SOC

And if we click again, we again come up with a different order:

TRIMBLE, JOHN R E

HAIRSTON, MAXINE C E

KAUFMANN, RALPH J E

HAIRSTON, MAXINE C E

MCDONALD, STEPHEN L ECO

JANNUZI, F TOMASSON ECO

BROWNING, GRAYSON D PHL

BARNES, WARNER J S/P

VENTO, ARNOLD C S/P

GALLE, OMER R SOC


The point is that it should not do that. On a second click the table should not change. case in point: if we want to display the names in alphabetical order per dept. In IE that is simple: you click on the sort by name, and then on sort by dept. Voila. FF does not retain the first sort and creates something random, hence the different order of items after a repeated exactly same sort.
Copy linkTweet thisAlerts:
@Orc_ScorcherJan 31.2008 β€”Β The point is that it should not do that. [/quote]Strictly speaking you're wrong because sort() is not guaranteed by the language spec to be stable, which means that things which compare equal do not have to keep their order. However, others found it annoying and preferred IE's behaviour, too, so Firefox 3 switches to a stable sort.

If you need this to be backwards compatible with Firefox 2 (or other browsers, which might not have implemented a stable sort either), change your sortColumn so that it never returns 0.
Copy linkTweet thisAlerts:
@tvhaustinauthorJan 31.2008 β€”Β I knew I would get in trouble by saying FF did it wrong. haha

But yes that explains it, and that is the obvious solution. very cool, will implement that right away.

Thanks for the help!
Copy linkTweet thisAlerts:
@tvhaustinauthorJan 31.2008 β€”Β hmmm OK I changed the

return ((x < y) ? -1 : ((x > y) ? 1 : 0));

into

return ((x < y) ? -1 : 1 );

And the result is that now in both browsers I have a non-stable sort. How can I create a stable sort in FF 2? Or do I just have to wait for FF3 tom come out? ?
Copy linkTweet thisAlerts:
@Orc_ScorcherJan 31.2008 β€”Β The algorithm goes like this:[LIST=1]
  • [*]Compute the result R of the comparison of the items in the primary column.
  • [*]If R is not 0, return R.
  • [*]Return the result of he comparison of the items in the secondary column.

  • [/LIST]
    Γ—

    Success!

    Help @tvhaustin spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 β€” BETA 6.18,
    whats_new: community page,
    up_next: more Davinciβ€’003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinciβ€’003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...