PDA

View Full Version : sort array alphabetically


crustylog
10-11-2002, 05:19 PM
can anyone help a newbie.
all i want to do is sort the items in the list to show alphabetically.
there will be my own data showing, but this is the script.

i've tried combo1.sort() , but it does'nt seem to be working.

any help would be great. Thanks


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<form name="dynamiccombo">
<select name="stage2" size="1">
<option value="#">This is a Place Holder</option>
<option value="#">This is a Place Holder</option>
<option value="#">This is a Place Holder</option>
</select>
<input type="button" name="test" value="Go!"
onClick="gothere()">
</form>

<script>
<!--

//Dynamic combo box script- by javascriptkit.com
//Visit JavaScript Kit (http://javascriptkit.com) for script
//Credit must stay intact for use


//DEFINE the group of links for display in the combo
//EXTEND each array and its elements as needed
//BE sure to follow the pattern revealed below
var combo1=new Array()
combo1[0]=new Option("JavaScript Kit","http://javascriptkit.com")
combo1[1]=new Option("Dynamic Drive","http://www.dynamicdrive.com")
combo1[2]=new Option("Freewarejava.com","http://www.freewarejava.com")
combo1[3]=new Option("Free Web Templates","http://www.freewebtemplates.com")
combo1[4]=new Option("Web Monkey","http://www.webmonkey.com")


var combo2=new Array()
combo2[0]=new Option("CNN","http://www.cnn.com")
combo2[1]=new Option("MSNBC","http://www.msnbc.com")
combo2[2]=new Option("BBC News","http://news.bbc.co.uk")
combo2[3]=new Option("ABC News","http://www.abcnews.com")

var combo3=new Array()
combo3[0]=new Option("Hollywood.com","http://www.hollywood.com")
combo3[1]=new Option("MTV","http://www.mtv.com")
combo3[2]=new Option("ETOnline","http://etonline.com")


var cacheobj=document.dynamiccombo.stage2

function populate(x){
for (m=cacheobj.options.length-1;m>0;m--)
cacheobj.options[m]=null
selectedarray=eval(x)
for (i=0;i<selectedarray.length;i++)
cacheobj.options[i]=new Option(selectedarray[i].text,selectedarray[i].value)
cacheobj.options[0].selected=true
}

function gothere(){
location=cacheobj.options[cacheobj.selectedIndex].value
}

//SHOW first combo by default
populate(combo1)

//-->
</script>

<!--SET up your links, and pass in the name of the group (ie: combo1) you wish to display for the link in question-->
<a href="javascript:populate(combo1)">Webmaster sites</a> | <a href="javascript:populate(combo2)">News sites</a> | <a href="javascript:populate(combo3)">Entertainment</a>

</body>
</html>

requestcode
10-11-2002, 05:57 PM
I don't think that will work because combo1 contains objects rather that just text. You might have to place the links and text descriptions in a separte array sort that and then build your objects. Here is an example:
<html>
<head>
<title>Sort an Array</title>
<SCRIPT LANGUAGE="JavaScript">
var myarray=new Array()
myarray[0]="JavaScript Kit,http://javascriptkit.com"
myarray[1]="Dynamic Drive,http://www.dynamicdrive.com"
myarray[2]="Freewarejava.com,http://www.freewarejava.com"
myarray[3]="Free Web Templates,http://www.freewebtemplates.com"
myarray[4]="Web Monkey,http://www.webmonkey.com"
myarray.sort()
var combo1=new Array()
for(i=0;i<myarray.length;i++)
{
links=myarray[i].split(",")
combo1[i]=new Option(links[0],links[1])
}
alert(combo1[0].text+"\n"+combo1[1].text+"\n"+combo1[2].text+"\n"+combo1[3].text+"\n"+combo1[4].text)
</SCRIPT>
</head>
<body>

</body>
</html>

adios
10-11-2002, 05:58 PM
Add this line to your script:

function populate(x){
for (m=cacheobj.options.length-1;m>0;m--)
cacheobj.options[m]=null
selectedarray=eval(x)
selectedarray.sort(function(a, b) {return (a.text>b.text) ? 1 : (b.text>a.text) ? -1 : 0 });
......etc.

crustylog
10-11-2002, 06:14 PM
Big THANKS adios, that 1 line of code makes it work a treat.
havn't got a clue what it means, but i'll learn.

also thanks Requestcode for your help.

adios
10-11-2002, 06:21 PM
http://www.faqts.com/knowledge_base/view.phtml/aid/1531/fid/144 :cool: