richardss
10-03-2005, 10:43 AM
I have a script which uses components in our content management system to create an array of titles and sort the array of titles. What I need to do is separate the titles into sections, each prefaced with a row that has the letter of the alphabet:
A
Administration
Auto Mechanics
B
Business Administration
C
Cooking
Here is the script that sorts the array. I've tried several things to get the A, B, C rows in, but none of them work. Does anything know how to do this?
I'm very rusty with javascript.
<SCRIPT LANGUAGE="JavaScript">
// The array that will hold the articles:
var articleList = new Array();
var i = 0;
// this function creates Article objects
function Article(PostedDate, Title, Url, Created) {
this.PostedDate= PostedDate;
this.Title = Title;
this.Url = Url;
this.Created = Created;
}
</SCRIPT>
<script language="JavaScript">
//Records pulled by the AssetQuery component are then added to an array:
articleList[i] = new Article("$node.contribution 'PostedDate')","$node.contribution 'Title')","$node.url","$node.CreateDate");
i++;
</script>
<SCRIPT LANGUAGE="JavaScript">
// this function will sort Article objects by title:
function comparetitles(a1,a2) {
return a1.title < a2.title ? -1:
a1.title == a2.title ? 0 : 1;
}
// this call will sort the array of Articles by the title:
articleList.sort(comparetitles);
// this will print the contents of the array:
for (j=0; j < articleList.length; j++) {
document.write("<tr><td align=left valign=top><a class=navbluecopy href='" + articleList[j].Url + "'>" + articleList[j].Title + "</a></td></tr>");
}
</SCRIPT>
A
Administration
Auto Mechanics
B
Business Administration
C
Cooking
Here is the script that sorts the array. I've tried several things to get the A, B, C rows in, but none of them work. Does anything know how to do this?
I'm very rusty with javascript.
<SCRIPT LANGUAGE="JavaScript">
// The array that will hold the articles:
var articleList = new Array();
var i = 0;
// this function creates Article objects
function Article(PostedDate, Title, Url, Created) {
this.PostedDate= PostedDate;
this.Title = Title;
this.Url = Url;
this.Created = Created;
}
</SCRIPT>
<script language="JavaScript">
//Records pulled by the AssetQuery component are then added to an array:
articleList[i] = new Article("$node.contribution 'PostedDate')","$node.contribution 'Title')","$node.url","$node.CreateDate");
i++;
</script>
<SCRIPT LANGUAGE="JavaScript">
// this function will sort Article objects by title:
function comparetitles(a1,a2) {
return a1.title < a2.title ? -1:
a1.title == a2.title ? 0 : 1;
}
// this call will sort the array of Articles by the title:
articleList.sort(comparetitles);
// this will print the contents of the array:
for (j=0; j < articleList.length; j++) {
document.write("<tr><td align=left valign=top><a class=navbluecopy href='" + articleList[j].Url + "'>" + articleList[j].Title + "</a></td></tr>");
}
</SCRIPT>