Sorting an Array - Ignore first 3 characters

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • johkar

    #1

    Sorting an Array - Ignore first 3 characters

    I am in the process of editing the below code I found online, I have a
    from multi-select list and a to multi-select list. Before rewriting
    the to list, I want to sort it but ignore the "F - " and the "R - ".
    Ideas???
    F - A200000 Some Text Here
    R - H205200 Some Other Text

    John

    function move(fbox, tbox) {
    var arrFbox = new Array();
    var arrTbox = new Array();
    var myTo=""
    var arrLookup = new Array();
    var i;
    for (i = 0; i < tbox.options.le ngth; i++) {
    arrLookup[tbox.options[i].text] = tbox.options[i].value;
    arrTbox[i] = tbox.options[i].text;
    myTo += tbox.options[i].value;
    }
    var fLength = 0;
    var tLength = arrTbox.length;
    for(i = 0; i < fbox.options.le ngth; i++) {
    arrLookup[fbox.options[i].text] = fbox.options[i].value;
    if (fbox.options[i].selected && fbox.options[i].value != "" &&
    myTo.indexOf(fb ox.options[i].value) <= -1) {
    arrTbox[tLength] = fbox.options[i].text;
    tLength++;
    }
    else {
    arrFbox[fLength] = fbox.options[i].text;
    fLength++;
    }
    }
    //arrFbox.sort();
    arrTbox.sort();
    //fbox.length = 0;
    tbox.length = 0;
    var c;
    //for(c = 0; c < arrFbox.length; c++) {
    //var no = new Option();
    //no.value = arrLookup[arrFbox[c]];
    //no.text = arrFbox[c];
    //fbox[c] = no;
    //}
    for(c = 0; c < arrTbox.length; c++) {
    var no = new Option();
    no.value = arrLookup[arrTbox[c]];
    no.text = arrTbox[c];
    tbox[c] = no;
    }
    }
  • Jc

    #2
    Re: Sorting an Array - Ignore first 3 characters

    Sure, specify your own sort function to use. The Array's sort method by
    default just does a basic sort, so if you want anything fancier, you
    can write a custom function to compare two values that ignores the
    first three characters, and pass that in to the sort method as a
    parameter.

    See
    http://msdn.microsoft.com/library/de...6jsmthsort.asp

    Comment

    • Fred Oz

      #3
      Re: Sorting an Array - Ignore first 3 characters

      johkar wrote:[color=blue]
      > I am in the process of editing the below code I found online, I have a
      > from multi-select list and a to multi-select list. Before rewriting
      > the to list, I want to sort it but ignore the "F - " and the "R - ".
      > Ideas???
      > F - A200000 Some Text Here
      > R - H205200 Some Other Text[/color]
      [snip]

      Seems to me you want to trim 4 characters "F - " and "R - ". I haven't
      played with the JS sort function, but some act funny if you have
      leading spaces (but it may not be an issue here, suck it and see).

      I would trim the required number characters from the start of each
      string and append them to the end:

      'F - A200000' becomes 'A200000F - '

      then sort them, then trim the same number from the end and put them
      back onto the start. Again, watch for truncated spaces.

      Cheers, Fred.

      Comment

      • Zifud

        #4
        Re: Sorting an Array - Ignore first 3 characters

        johkar wrote:[color=blue]
        > I am in the process of editing the below code I found online, I have a
        > from multi-select list and a to multi-select list. Before rewriting
        > the to list, I want to sort it but ignore the "F - " and the "R - ".
        > Ideas???
        > F - A200000 Some Text Here
        > R - H205200 Some Other Text[/color]
        [snip]

        I gotta ask - where is the data coming from? If this is a one-of, sort
        it using any method you like - Word or Excel, or in *nix pipe together
        some cut/paste/sort commands - then create your option list and it's done.

        If the data is coming from a database, do it on the server. I'm certain
        an SQL newsgroup will give you a query to do what you want. Then your
        list is already sorted before it gets to your page.

        I can't for the life of me see why you would be sorting a list of stuff
        like this on the client.

        Zif.

        Comment

        • johkar

          #5
          Re: Sorting an Array - Ignore first 3 characters

          nosendjunk@msn. com (johkar) wrote in message news:<e5daa16c. 0410211737.2767 e203@posting.go ogle.com>...[color=blue]
          > I am in the process of editing the below code I found online, I have a
          > from multi-select list and a to multi-select list. Before rewriting
          > the to list, I want to sort it but ignore the "F - " and the "R - ".
          > Ideas???
          > F - A200000 Some Text Here
          > R - H205200 Some Other Text
          >
          > John
          >
          > function move(fbox, tbox) {
          > var arrFbox = new Array();
          > var arrTbox = new Array();
          > var myTo=""
          > var arrLookup = new Array();
          > var i;
          > for (i = 0; i < tbox.options.le ngth; i++) {
          > arrLookup[tbox.options[i].text] = tbox.options[i].value;
          > arrTbox[i] = tbox.options[i].text;
          > myTo += tbox.options[i].value;
          > }
          > var fLength = 0;
          > var tLength = arrTbox.length;
          > for(i = 0; i < fbox.options.le ngth; i++) {
          > arrLookup[fbox.options[i].text] = fbox.options[i].value;
          > if (fbox.options[i].selected && fbox.options[i].value != "" &&
          > myTo.indexOf(fb ox.options[i].value) <= -1) {
          > arrTbox[tLength] = fbox.options[i].text;
          > tLength++;
          > }
          > else {
          > arrFbox[fLength] = fbox.options[i].text;
          > fLength++;
          > }
          > }
          > //arrFbox.sort();
          > arrTbox.sort();
          > //fbox.length = 0;
          > tbox.length = 0;
          > var c;
          > //for(c = 0; c < arrFbox.length; c++) {
          > //var no = new Option();
          > //no.value = arrLookup[arrFbox[c]];
          > //no.text = arrFbox[c];
          > //fbox[c] = no;
          > //}
          > for(c = 0; c < arrTbox.length; c++) {
          > var no = new Option();
          > no.value = arrLookup[arrTbox[c]];
          > no.text = arrTbox[c];
          > tbox[c] = no;
          > }
          > }[/color]


          Thank you all for your responses. This is what I came up with. It
          seems to work during brief testing. Let me know if you see flaws.

          Change arrTbox.sort();
          to arrTbox.sort(co mpare);

          function compare(a,b){
          a = a.substr(4);
          b = b.substr(4);
          if(a < b) return -1;
          if(b < a) return 1;
          if(a == b) return 0;
          }

          As far as Zifud's question as to why I would ever want to do this on
          the client. Consider two multiple select lists where you transfer
          items back and forth. I realize there are different schools of
          thought on what you should do on the client. Zifud, you probably
          subscribe to hitting the server for most everything, I am more open
          depending on the situation. It just comes down to your audience,
          environment and business requirements. Thanks again.

          John

          Comment

          • Dr John Stockton

            #6
            Re: Sorting an Array - Ignore first 3 characters

            JRS: In article <1098415684.520 985.37770@f14g2 000cwb.googlegr oups.com>,
            dated Thu, 21 Oct 2004 20:28:04, seen in news:comp.lang. javascript, Jc
            <google@weinric hs.com> posted :[color=blue]
            >Sure, specify your own sort function to use. The Array's sort method by
            >default just does a basic sort, so if you want anything fancier, you
            >can write a custom function to compare two values that ignores the
            >first three characters, and pass that in to the sort method as a
            >parameter.
            >
            >See
            >http://msdn.microsoft.com/library/de...cript56/html/j
            >s56jsmthsort.a sp
            >[/color]

            However, the compare function is called more than o(N) times when
            sorting a N-element array; and the required function, though not
            difficult, takes non-trivial time.

            Therefore, if N is sufficiently large (and that may not be very big) it
            should be quicker to use a RegExp or substring method to park the first
            three characters at the end of the value in time o(N), do a standard
            sort, and restore the data.

            --
            © John Stockton, Surrey, UK. ?@merlyn.demon. co.uk Turnpike v4.00 IE 4 ©
            <URL:http://www.jibbering.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
            <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
            <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

            Comment

            • Zifud

              #7
              Re: Sorting an Array - Ignore first 3 characters

              johkar wrote:
              [snip][color=blue]
              > Change arrTbox.sort();
              > to arrTbox.sort(co mpare);
              >
              > function compare(a,b){
              > a = a.substr(4);
              > b = b.substr(4);
              > if(a < b) return -1;
              > if(b < a) return 1;
              > if(a == b) return 0;
              > }
              >
              > As far as Zifud's question as to why I would ever want to do this on
              > the client. Consider two multiple select lists where you transfer
              > items back and forth. I realize there are different schools of
              > thought on what you should do on the client. Zifud, you probably
              > subscribe to hitting the server for most everything,[/color]

              Not at all, I just couldn't see the reason for doing it. Your scenario
              seems quite reasonable, now I understand.

              As Dr. J suggests, your bubble sort will run into performance issues at
              some point, but if your list my not get long enough (maybe it's always
              less than 10 items or such).

              Another suggestion is to give each option a value equivalent to the key
              you wish to sort on, then use that to sort the array. When you post
              the selected & sorted list back to the server, use the text rather than
              the value at the server end.

              Cheers, Zif.

              Comment

              Working...