Click to See Complete Forum and Search --> : Sorting arrays


Webskater
12-01-2003, 09:26 AM
I have two arrays thus:
var UserIDAry = new Array();
var UserNameAry = new Array();

These might be populated like this:
UserIDAry[0] = 0
UserIDAry[1] = 1
UserIDAry[2] = 2

UserNameAry[0] = 'Zak'
UserNameAry[1] = 'Norman'
UserNameAry[2] = 'Arthur'

Which means Zak has a UserID of 0, Norman has a UserID of 1 and Arther has a UserID of 2.

I want to populate a select box with the two arrays - to show the names and put the IDs as the values in the option tags. Can anyone tell me how I sort the UserNameAry so that it displays in alphabetical order (ascending)? ...

... and how can I sort the UserIDAry so that the right values are shown against the right people so it will look like ...
<select>
<option value=2>Arthur</option>
<option value=1>Norman</option>
<option value=0>Zak</option>
</select>

Thanks for any help. (I have my own reasons for not putting them both in the same array).

clairec666
12-01-2003, 09:31 AM
I can't work out the exact code at the moment, but if you know JavaScript well, this should get you started. If not, I can work out a proper version for you in a while.

var length1 = UserNameAry.length

Use document.write() to write a select box directly into your HTML. You will need to use a 'for' loop to insert the values of the names:
for(i=0; i<length1; i++) {
var contents += </option>
var contents += UserNamAry[i];
contents += "</option>
}

Does this help at all?

Gollum
12-01-2003, 09:39 AM
I can't think of any simple way to simultaneously sort two arrays at the same time and in the same way.

Your best alternative is to put the two bits of data together into objects.

so your code might look like this...

// declare an array of objects with all the user data...
var aUsers =
[
{id:0,name:"Zak"},
{id:1,name:"Norman"},
{id:2,name:"Arthur"}
];

function cmpUserNames(a,b)
{
if ( a.name < b.name) return -1;
if ( a.name > b.name) return 1;
return 0;
}

// sort the array
aUsers.sort(cmpUserNames);

// now create the select box
document.write('<select name="users">');
for ( var i = 0; i < aUsers.length; i++ )
{
document.write('<option value="' + aUsers[i].id + '">' + aUsers[i].name);
}
document.write('</select>');

clairec666
12-01-2003, 09:42 AM
I've done the following (script seen below) which puts the information onto the page, but this doesn't sort them into order (see Gollum's method).
My script isn't working on my computer at the moment. Not sure why, if you spot the problem, please tell me!

<script type="text/javascript">
var UserNameAry = New Array();
var UserIDAry = new Array("1","2","3");

UserNameAry[0] = 'Zak';
UserNameAry[1] = 'Norman';
UserNameAry[2] = 'Arthur';

var contents = "<form><select>";
for (i=0; i<length1; i++) {
var contents += </option>
var contents += UserNameAry[i];
contents += "</option>
}
contents += "</select></form>";
document.write(contents);
</script>

Webskater
12-01-2003, 10:45 AM
Thanks for your answers. I'll have a play and see if I can get it to work.