Click to See Complete Forum and Search --> : Complicated array sort
Missy_K
12-18-2003, 12:27 PM
I need to sort an array...but not simply using the sort().
I have elements containing html mailto links...
user[0]=<h3><A HREF=MAILTO:Joe.Smith@world.com>Joe Smith</A></h3>
user[1]=<h3><A HREF=MAILTO:Joe.Rice@world.com>Joe Rice</A></h3>
I need to the array and put it in alphabetical order by last name. Rice, Smith...etc.
Can anyone help?
olerag
12-18-2003, 01:55 PM
Here's one way...
1. Iterate thru the array and "substring" the portion of each
array element that contains the person's name.
2. Using a "bubble sort", put the "ordered" results into a
new "alphabetized" array.
If you need help with the actual coding, let me know and I'll
come up with something for you.
Missy_K
12-18-2003, 02:19 PM
I got the substring ok, and I can pull out my lastname:
var start = employees[counter2].indexOf(".")
var end = employees[counter2].indexOf("@")
var lastname = employees[counter2].substring(start+1,end)
but you lost me on the bubble sort thing...could you help me out???
olerag
12-18-2003, 03:46 PM
Here's an example (last name substring is in its own JS
function ( getLastName()) )....I'm using the same convention
of the "String" format as you originally provided and the list
contains, I believe, 8 names.
The list is a multiple selection listing. Pick some or all and
then "hit the button" to see the results. Have fun...
<html>
<head>
<script type="text/javascript">
function sortResults(form) {
var count = 0;
var listObj = form.urlList;
var sortArray = new Array;
var temp;
for (var i=0;i<listObj.length;i++) {
if (listObj.options[i].selected) {
sortArray[count] = listObj.options[i].value;
count++;
}
}
if (sortArray.length > 0) {
var unsorted = false;
var thisVal;
var nextVal;
do {
unsorted = false;
for(var i=0; i<sortArray.length-1; i++) {
thisVal = getLastName(sortArray[i].toLowerCase());
nextVal = getLastName(sortArray[i+1].toLowerCase());
if (thisVal > nextVal) {
temp = sortArray[i];
sortArray[i] = sortArray[i+1];
sortArray[i+1] = temp;
unsorted = true;
}
}
} while(unsorted);
}
if (sortArray.length > 0) {
temp = "";
for (var i=0; i<sortArray.length; i++) {
temp += sortArray[i] + "\n";
}
alert("Sort Results\n" + temp);
}
}
function getLastName(stringIn) {
var startPos = stringIn.indexOf(".");
var endPos = stringIn.indexOf("@world");
return(stringIn.substring(startPos+1,endPos));
}
</script>
</head>
<body>
<center>
<b>Array Sort Demo</b>
<form>
<select name="urlList" size="10" multiple>
<option value="<h3><A HREF=MAILTO:Joe.Smith@world.com>Joe Smith</A></h3>">Joe Smith</option>
<option value="<h3><A HREF=MAILTO:Tom.Thumb@world.com>Tom.Thumb</A></h3>">Tom Thumb</option>
<option value="<h3><A HREF=MAILTO:John.Rice@world.com>John.Rice</A></h3>">John Rice</option>
<option value="<h3><A HREF=MAILTO:Kobe.Bryant@world.com>Kobe.Bryant</A></h3>">Kobe Bryant</option>
<option value="<h3><A HREF=MAILTO:Frodo.Baggins@world.com>Frodo.Baggins</A></h3>">Frodo Baggins</option>
<option value="<h3><A HREF=MAILTO:Bugs.Bunny@world.com>Bugs.Bunny</A></h3>">Bugs Bunny</option>
<option value="<h3><A HREF=MAILTO:Edgar.Poe@world.com>Edgar.Poe</A></h3>">Edgar Poe</option>
<option value="<h3><A HREF=MAILTO:Frank.Zappa@world.com>Frank.Zappa</A></h3>">Frank Zappa</option>
</select>
<br>
<input type="button" value="Sort Results" onClick="sortResults(this.form)">
</form>
<hr>
</center>
</body>
</html>