/    Sign up×
Community /Pin to ProfileBookmark

Anyone know how .sort() method does it’s thing?

I just finished writing my own sort routine for some arrays in my web page because I couldn’t get the .sort() method to work.

Having never used the method before this, I used an example from Danny Goodman’s [U]JavaScript Bible[/U] to code the following snippets:

[code]
function compare(a, b) {

return (a.cmd < b.cmd) ? -1 : 1;

}

for (indx = 0; indx < nodeTbl.length; indx++) {
tblObj = eval(nodeTbl[indx] + “Cmds”);
tblObj.sort(compare);
}
[/code]

The result was arrays with elements in the reverse order. I’ve probably missed something, but I don’t see it. I added a [b]debugger;[/b] line before the return in the compare function to see what was being passed as [b]a[/b] and [b]b[/b] but it was never envoked. Another mystery? So, my question is this: Does anyone have a good understanding of what this method is actually doing?

Perhaps it was due to the fact that the arrays were being reversed rather than sorted, but my sort routine actually seems to be a bit faster so I’m not unhappy with the results. However, the .sort() method may be useful sometime in the future, if I can figure out how to make it work. ?

Should it have some bearing on the problem, my arrays are created with the following server side generated code:

[code]
var nodeTbl = new Array(‘DEVEL’,’IBAT’,’IOMH’,’ISAT’,’OSAT’,’PRDA’,’PRDB’,’WESTP’);
var DEVELCmds = new Array();
var IBATCmds = new Array();
var IOMHCmds = new Array();
var ISATCmds = new Array();
var OSATCmds = new Array();
var PRDACmds = new Array();
var PRDBCmds = new Array();
var WESTPCmds = new Array();
DEVELCmds[0] = new cmdData(‘PATHCOM $PCOM;STATUS SERVER’,’S PW ‘,’Y’,’SUPERSUPP’);
IBATCmds[0] = new cmdData(‘PATHCOM $PCOM;STATUS SERVER’,’S PW ‘,’Y’,’SUPERSUPP’);
IOMHCmds[0] = new cmdData(‘PATHCOM $PCOM;STATUS SERVER’,’S PW ‘,’Y’,’SUPERSUPP’);
ISATCmds[0] = new cmdData(‘PATHCOM $PCOM;STATUS SERVER’,’S PW ‘,’Y’,’SUPERSUPP’);
OSATCmds[0] = new cmdData(‘PATHCOM $PCOM;STATUS SERVER’,’S PW ‘,’Y’,’SUPERSUPP’);
PRDACmds[0] = new cmdData(‘PATHCOM $PCOM;STATUS SERVER’,’S PW ‘,’Y’,’SUPERSUPP’);
PRDBCmds[0] = new cmdData(‘PATHCOM $PCOM;STATUS SERVER’,’S PW ‘,’Y’,’SUPERSUPP’);
WESTPCmds[0] = new cmdData(‘PATHCOM $PCOM;STATUS SERVER’,’S PW ‘,’Y’,’SUPERSUPP’);
DEVELCmds[1] = new cmdData(‘STATUS’,’TR ‘,’Y’,’SUPERSUPP’);
IBATCmds[1] = new cmdData(‘STATUS’,’TR ‘,’Y’,’SUPERSUPP’);
IOMHCmds[1] = new cmdData(‘STATUS’,’TR ‘,’Y’,’SUPERSUPP’);
ISATCmds[1] = new cmdData(‘STATUS’,’TR ‘,’Y’,’SUPERSUPP’);
OSATCmds[1] = new cmdData(‘STATUS’,’TR ‘,’Y’,’SUPERSUPP’);
PRDACmds[1] = new cmdData(‘STATUS’,’TR ‘,’Y’,’SUPERSUPP’);
PRDBCmds[1] = new cmdData(‘STATUS’,’TR ‘,’Y’,’SUPERSUPP’);
WESTPCmds[1] = new cmdData(‘STATUS’,’TR ‘,’Y’,’SUPERSUPP’);
IBATCmds[2] = new cmdData(‘GOUMP’,’TO ‘,’N’,’SUPERSUPP’);
IBATCmds[3] = new cmdData(‘PATHCOM $IBPD;ABORT TERM’,’S PW ‘,’Y’,’SUPERSUPP’);
IBATCmds[4] = new cmdData(‘PATHCOM $IBPD;START TERM’,’S PW ‘,’Y’,’SUPERSUPP’);
IBATCmds[5] = new cmdData(‘PATHCOM $IBTR;ABORT TERM’,’S PW ‘,’Y’,’SUPERSUPP’);
IBATCmds[6] = new cmdData(‘PATHCOM $IBTR;START TERM’,’S PW ‘,’Y’,’SUPERSUPP’);
IBATCmds[7] = new cmdData(‘PATHCOM $IVPD;ABORT TERM’,’S PW ‘,’Y’,’SUPERSUPP’);
IBATCmds[8] = new cmdData(‘PATHCOM $IVPD;START TERM’,’S PW ‘,’Y’,’SUPERSUPP’);
IBATCmds[9] = new cmdData(‘RESETCAM’,’TR ‘,’N’,’SUPERSUPP’);
IBATCmds[10] = new cmdData(‘RSTRTCAM’,’TR ‘,’N’,’SUPERSUPP’);
// etc., etc. ….
[/code]

and this function from my .js file.

[code]
function cmdData(cmd, cmdcode, preq, svr) {
// custom object constructor
this.cmd = cmd;
this.cmdcode = cmdcode;
this.preq = preq;
this.svr = svr;

return;

}
[/code]

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@CharlesSep 01.2004 — Two observations:

  • * What's up with the "eval()" function?

  • * Your sorting function needs to return 0 if the two are the same.


  • [font=monospace]function compare(a, b) {

    return a.cmd == b.cmd ? 0 : (a.cmd < b.cmd ? -1 : 1);

    }

    for (indx = 0; indx < nodeTbl.length; indx++) {
    tblObj = nodeTbl[indx] + "Cmds";
    tblObj.sort(compare);
    }[/font]
    Copy linkTweet thisAlerts:
    @ccoderauthorSep 01.2004 — [i]Originally posted by Charles [/i]

    [B]Two observations:



  • * What's up with the "eval()" function?

  • * Your sorting function needs to return 0 if the two are the same.


  • [/B][/QUOTE]

    The entries in nodeTbl are system names. This list will vary over time as we consolidate smaller systems into larger super nodes. Similarly, the number of arrays of commands will vary over time. I need to dynamically create and reference the arrays containing the commands that can be sent to the various systems (found in nodeTbl) from my web page. The array nodeTbl is used to build a SELECT list containing the available systems. The selected system is then used to build/rebuild another SELECT list. Although I have been programming for 30 years, I am relatively new to JavaScript and DHTML. I seem to recall some negative comments regarding eval but at the moment I don't remember what they were. Is there a better way to do this?

    As I stated, I used an example web page that employs the sort() method. The code in the page works with no test for equality. Recall that my main question was "how does the method work?". I would post the example web page that I used but I'm not sure if that would be legal.
    Copy linkTweet thisAlerts:
    @CharlesSep 01.2004 — [i]Originally posted by ccoder [/i]

    [B]The entries in nodeTbl are system names. This list will vary over time as we consolidate smaller systems into larger super nodes. Similarly, the number of arrays of commands will vary over time. I need to dynamically create and reference the arrays containing the commands that can be sent to the various systems (found in nodeTbl) from my web page. The array nodeTbl is used to build a SELECT list containing the available systems. The selected system is then used to build/rebuild another SELECT list. Although I have been programming for 30 years, I am relatively new to JavaScript and DHTML. I seem to recall some negative comments regarding eval but at the moment I don't remember what they were. Is there a better way to do this?



    As I stated, I used an example web page that employs the sort() method. The code in the page works with no test for equality. Recall that my main question was "how does the method work?". I would post the example web page that I used but I'm not sure if that would be legal. [/B]
    [/QUOTE]
    Did you give my example a try?
    Copy linkTweet thisAlerts:
    @ccoderauthorSep 01.2004 — [i]Originally posted by Charles [/i]

    [B]Did you give my example a try? [/B][/QUOTE]

    Just finished testing it!

    Had to change [b]tblObj = nodeTbl[indx] + "Cmds";[/b] back to [b]tblObj = eval(nodeTbl[indx] + "Cmds");[/b]. With your code, tblObj is a string with value "DEVELCmds", not a reference to the array DEVELCmds.

    At any rate, the sort worked. This is surprising to me for 2 reasons:

    1) there are no duplicate values for [b]cmd[/b] so the function will never return zero.

    2) the authors example doesn't test for equality and it works.

    So, I guess my question still stands. What in the world is going on inside the sort() method?

    Gotta run to a meeting!
    Copy linkTweet thisAlerts:
    @ccoderauthorSep 02.2004 — Well, after I returned from the meeting I did a search on eval to see what the fuss is all about. In one post I found an interesting link to a page on [url="http://www.litotes.demon.co.uk/js_info/sq_brks2.html"]JavaScript Square Bracket Notation[/url]. I also used the link to DevEdge that TheBearMay provided to see what it had to say about arrays and then looked up eval to see what what I could find. Aside from the fact that it was removed as a method of object in 1.4 and some caveats about its inefficiencies, I found nothing against using it as a top level function.

    Anyway, I did learn a few things in the page on Square Bracket Notation. I have used the syntax, but always thought of it as a method of indexing. Based on what I read, it appeared that I might be able replace the call to eval by wrapping the string in "[]". Close, but not quite. It appears to have created a new array with a single element (see case 2 below) rather than a reference to the array that I wanted to sort.

    I have included some test results below. In each case, I put the code in debug and added a watch on tblObj. The results shown are for the first element in nodeTbl: indx = 0, nodeTbl[indx] = “DEVEL”. (Please ignore my use of the code tags - I'm not aware of any other way to maintain the columns in the watch info)


    ----------------------------------------------------------------------</H2>
    Case 1:

    for (indx = 0; indx &lt; nodeTbl.length; indx++) {<br/>
    tblObj = nodeTbl[indx] + "Cmds";<br/>
    tblObj.sort(compare);<br/>
    }

    Watch on tblObj<br/>
    Name Value Type<br/>
    tblObj "DEVELCmds" String

    Result: ‘Object doesn’t support this property or method’ occurred when
    <H2>attempting to excute sort.
    ----------------------------------------------------------------------</H2>
    Case 2:

    for (indx = 0; indx &lt; nodeTbl.length; indx++) {<br/>
    tblObj = [nodeTbl[indx] + "Cmds"];<br/>
    tblObj.sort(compare);<br/>
    }

    Watch on tblObj<br/>
    Name Value Type<br/>
    - tblObj Object<br/>
    0 "DEVELCmds" String

    <H2>Result: arrays were unchanged.
    ----------------------------------------------------------------------</H2>
    Case 3:

    for (indx = 0; indx &lt; nodeTbl.length; indx++) {<br/>
    tblObj = eval(nodeTbl[indx] + "Cmds");<br/>
    tblObj.sort(compare);<br/>
    }

    Watch on tblObj<br/>
    Name Value Type<br/>
    - tblObj Object<br/>
    - 0 Object<br/>
    cmd "PATHCOM $PCOM;STATUS SERVER" String<br/>
    cmdcode "S PW String<br/>
    preq "Y" String<br/>
    svr "SUPERSUPP" String<br/>
    - 1 Object<br/>
    cmd "STATUS" String<br/>
    cmdcode "TR " String<br/>
    preq "Y" String<br/>
    svr "SUPERSUPP" String

    <H2>Result: arrays were sorted correctly.
    ----------------------------------------------------------------------</H2>


    So, is anyone more familier with Square Bracket Notation? Is there another way of coding case 2 so that it will work for me?

    ×

    Success!

    Help @ccoder 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 5.4,
    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: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,

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