Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Oct 2003
    Location
    SC
    Posts
    936
    Thanks
    0
    Thanked 0 Times in 0 Posts

    A script that can alphabetize

    thats what I need, is it out there, did anyone do it?

    please, this would be very usefull

    more specifically what it should be able to do:

    <div class="alignLeft">non</div><div class="alignRight">not</div><div class="alignCenter">=</div>

    it should sort all these divs alphabeticaly (the left div counts)

    and then I guess put them back out in the same form, just ordered

    actually, come to thing of it, it doesnt have to be JS

    thanks people

  • #2
    New Coder
    Join Date
    Mar 2005
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You can use the arry.sort() method to do simple alphabetical sorting.

    For example...
    Code:
    function sortMyDivs(){
    
    var sortarray = new Array();
    
     for (i=0;i<nDivs;++i) { //where i is the id of the div??
          textVal = document.getElementById(i).innerText; //do this better
          sortarray[i] = new SortItem(i,textVal);
        }
    
    sortarray.sort(compare);
    //now write out the results!
    }
    
    function SortItem(objID, textVal)
    {
        this.ID = objID;
        this.val = textVal;
    }
    
    function compare(obj1, obj2)
    {
       if(obj1.val>obj2.val) {return 1;}
      else return 0;
    
    }
    This will hopefully sort the objects alphbetically by the innertext value. I could write the whole thing properly but I've run out of time.

    Good luck!

    wmjb.
    "It doesn't matter if I'm optomistic or not, nothing ever works out for me."

  • #3
    New Coder
    Join Date
    Mar 2005
    Posts
    61
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You can make the compare function compare anything from the objects you like. You can also have different compare functions for example... numeric sort, reverse alphabetic etc.
    "It doesn't matter if I'm optomistic or not, nothing ever works out for me."

  • #4
    Kor
    Kor is offline
    Red Devil Mod Kor's Avatar
    Join Date
    Apr 2003
    Location
    Bucharest, ROMANIA
    Posts
    8,478
    Thanks
    58
    Thanked 379 Times in 375 Posts
    eeerr, I would not use innerText, as it is an IE only method. Use innerHTML, or firstChild.nodeValue or better firstChild.data instead.
    KOR
    Offshore programming
    -*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •