Please help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • dungping5@yahoo.com

    Please help

    Hi,

    I am very new to Javascript, and have some problems with the code for
    sorting. My question is in the following web page:



    Thanks for help.

    Juli Zhang

  • Dietmar Meier

    #2
    Re: Please help

    dungping5@yahoo .com wrote:
    [color=blue]
    > I am very new to Javascript, and have some problems with the code for
    > sorting. My question is in the following web page: [...][/color]

    Untested quickhack for IDs matching /\D+-\d+/:

    function sortByID(a, b) {
    var A = a["ID"].toLowerCase(). replace(/\d/g,""),
    B = b["ID"].toLowerCase(). replace(/\d/g,""),
    C = a["ID"].replace(/\D/g,""),
    D = b["ID"].replace(/\D/g,"");
    if (A < B) return -1;
    else if (A > B) return 1;
    else if (C - D < 0) return -1;
    else if (C - D > 0) return 1;
    else return 0;
    }

    In addition to that, you should test if a function exists that
    is named like the value you get from the location's search before
    you call it:

    var sortCriteria = location.search .substring(1);
    if (sortCriteria && typeof window[sortCriteria] == "function")
    kidney.sort(win dow[sortCriteria]);

    ciao, dhgm

    Comment

    • dungping5@yahoo.com

      #3
      Re: Please help

      Dear Dietmar Meier:

      Thank you very, very much. The code works perfectly. It is now
      sorting correctly:



      Juli Zhang

      Comment

      • Dietmar Meier

        #4
        Re: Please help

        dungping5@yahoo .com wrote:
        [color=blue]
        > The code works perfectly. It is now sorting correctly: [...][/color]

        There's still an error in the other function, sortByPinyin().

        Replace

        var A = a["Pinyin"].toString.toLow erCase();
        var B = b["Pinyin"].toString.toLow erCase();

        with

        var A = a["Pinyin"].toString().toL owerCase();
        var B = b["Pinyin"].toString().toL owerCase();

        or, since all these values are of type "string" already, simply

        var A = a["Pinyin"].toLowerCase();
        var B = b["Pinyin"].toLowerCase();

        in lines 134 and 135.

        ciao, dhgm

        Comment

        • dungping5@yahoo.com

          #5
          Re: Please help


          Dietmar Meier wrote:[color=blue]
          > dungping5@yahoo .com wrote:
          >[color=green]
          > > The code works perfectly. It is now sorting correctly: [...][/color]
          >
          > There's still an error in the other function, sortByPinyin().
          >
          > Replace
          >
          > var A = a["Pinyin"].toString.toLow erCase();
          > var B = b["Pinyin"].toString.toLow erCase();
          >
          > with
          >
          > var A = a["Pinyin"].toString().toL owerCase();
          > var B = b["Pinyin"].toString().toL owerCase();
          >
          > or, since all these values are of type "string" already, simply
          >
          > var A = a["Pinyin"].toLowerCase();
          > var B = b["Pinyin"].toLowerCase();
          >
          > in lines 134 and 135.
          >
          > ciao, dhgm[/color]

          Errors corrected accordingly. Now it is in:



          Thanks again for expertise and kindness.

          Sincerely,
          Juli Zhang

          Comment

          Working...