Click to See Complete Forum and Search --> : How to Alphabetize an Array List?


BSquared18
08-27-2003, 10:02 PM
I was provided the following code to generate a list of the TV channels for all the interest boxes that are checked off.

Is there a way to alter the code so that the entire list is sorted in alphabetical order? (I figured out how to sort them alphabetically within each interest group--all the movie channels for example--but not for all the groups combined together when more than one box is checked off.)

Also, as the code stands now, the items for adjacent interest groups run together without a comma between them. Is there a way to have the comma occur between adjacent interest groups even when the whole list is sorted alphabetically?

The first of these two improvements has a higher priority if only one or the other is possible.

The code is:

<html>
<head>
<title>Untitled</title>
</head>

<body>

<script>
movieArr = new Array ("TCM","HBO","Starz","Cinemax")
newsArr = new Array ("Fox News","Headline News","CNN","ABC News")
sportsArr = new Array("ESPN","ESPN2","Fox Sports","CBS Sports")
childrensArr = new Array("Disney")
function showChannels(){
allInterests = document.myForm.interest
channels = ""
for (x=0; x<allInterests.length; x++){
if (allInterests[x].checked){
channels += eval(allInterests[x].value + "Arr")
}
}
alert(channels)
}
</script>

<form name="myForm">
<input type=checkbox name="interest" value="movie">movies
<input type=checkbox name="interest" value="news">news
<input type=checkbox name="interest" value="sports">sports
<input type=checkbox name="interest" value="childrens">childrens

<input type=button onClick="showChannels()" value="Get Channels">
</form>

</body>
</html>

Thanks!

Bill B.

gcrowan
08-27-2003, 10:28 PM
Not entirely sure what you are looking for.
Array.sort(), sorts the elements of an array. When sort() is called with no arguments, it sorts the array elements in alphabetical order.

BSquared18
08-28-2003, 06:24 AM
I'll try to clarify, and please keep in mind that I'm not a Javascript programmer, although I've done some programming in BASIC.

For simplicity's sake, suppose that the two arrays I have are:

movieArr = new Array ("A","C","E","G")
newsArr = new Array ("B","D","F","H")

Using the code I mentioned in the earlier post, is there a way to sort these two arrays so that if both the "movie" and "news" boxes are checked off, when the list is generated the items are arranged in alphabetical order: A, B, C, D, E, F, G, H? In other words, can the items within an array be moved around when the list is generated?

If the sort () function can be used to do this, exactly where would it go in the code posted earlier?

Thanks!

Bill B.

Charles
08-28-2003, 06:37 AM
<script type="text/javascript">
<!--
movieArr = new Array ("A","C","E","G")
newsArr = new Array ("B","D","F","H")
alert (movieArr.concat(newsArr).sort())
// -->
</script>

BSquared18
08-28-2003, 06:51 AM
Charles,

Yes, that's a step toward what I'm looking for! But when I place the line of code you suggest after the two arrays, the window with the list is opened immediately, showing those items, instead of pausing until I check off the boxes that determine which arrays will be listed.

Could you please copy and paste the code I posted originally, and insert your suggested code in a way that allows the boxes to be checked off before the alphabetized list is generated?

Thanks!

Bill B.