View Full Version : A script that can alphabetize

07-04-2005, 06:33 PM
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

07-05-2005, 07:38 AM
You can use the arry.sort() method to do simple alphabetical sorting.

For example...

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);

//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!


07-05-2005, 07:40 AM
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.

07-05-2005, 11:00 AM
eeerr, I would not use innerText, as it is an IE only method. Use innerHTML, or firstChild.nodeValue or better firstChild.data instead.