Forum Moderators: open

Message Too Old, No Replies

Sorting an array alphabetically

need to sort my keyword list by first letter

         

TinkTank

12:00 am on Mar 8, 2005 (gmt 0)

10+ Year Member



hello world,

looking for some help.
would like to have users view "keywords" from alphabetical list.

for example, instead of seing an alphabetical list 300 words long. I would like them to click on the linked letter "A" and have all keywords starting with the letter A returned.

first off, a little bg to help.
top of the page, I call master keyword list
===========

<%
library('global.np');

catalogName = Request.getParameter('searchCatalog');

if (catalogName.length == 0)
catalogName = CatalogSet.getAliases()[0];

fieldList = CatalogSet.get(catalogName).getTypeAll();
masterKeywordList = CatalogSet.getMasterKeywordsAll();

// Truncate the master keyword list if it is too long
masterKewordCount = masterKeywordList.length;
if (masterKeywordList.length > 128)
masterKeywordList = masterKeywordList.slice(0, 128);
%>

==========
further down the page i call the entire contents of the master keyword list for that page
==========

<% if (masterKeywordList.length > 0) { %>
<% processArray(masterKeywordList, %>

<A HREF="/netpub/server.np?quickfind=<%= masterKeywordList[x] %>&catalog=<%= catalogName %>&site=<%= encodeURI(Request.getParameter('site')) %>&template=results.np">
<%= masterKeywordList[x] %></A>
<BR>
<% ); %>
<% } %>

=========

i sure do hope this provides some info for you to work on. i will check back tonight to see any comments. Thank you so much!

-tink

SpaceFrog

9:38 am on Mar 8, 2005 (gmt 0)

10+ Year Member



an array can be sorted alphabetically with .sort()...

you would then just have to scan contents of array checking first letter and transfer wanted ones in a select ...

TinkTank

1:51 pm on Mar 8, 2005 (gmt 0)

10+ Year Member



Thanks for your reply:
"you would then just have to scan contents of array checking first letter and transfer wanted ones in a select ..."

but thats where I am getting caught up.
how do i script scan of the first letters only.

SpaceFrog

2:50 pm on Mar 8, 2005 (gmt 0)

10+ Year Member



for (i=0;i<myarray.length;i++){
if myarray[i].substr(0,1) ...

or it should look like that ...

TinkTank

3:36 pm on Mar 8, 2005 (gmt 0)

10+ Year Member



thanks for your help SF

ill try and work that in later today
and of course,
ill let you know how it works out

!:^)

SpaceFrog

3:47 pm on Mar 8, 2005 (gmt 0)

10+ Year Member



I skipped the part that send the items to select btu should you need any help on that part as well just ask ...

SpaceFrog

8:11 am on Mar 9, 2005 (gmt 0)

10+ Year Member



Previous replymust have been too long ...
here is a shorter version :

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Lorem ipsum dolor sit amet</title>
<script>
function populateselect(){
pickedOptions=document.getElementById('popdata').innerHTML.split(" ");
pickedOptions=pickedOptions.sort()

for (i=0;i<pickedOptions.length;i++){
newoption=document.createElement('option')
newoption.innerText=pickedOptions[i]
document.getElementById('alpha').appendChild(newoption)
}
}

function startingwith(){
document.getElementById('alpha').options.length=0
pickedOptions=document.getElementById('popdata').innerHTML.split(" ");
pickedOptions=pickedOptions.sort()

for (i=0;i<pickedOptions.length;i++){
var firstletter=document.getElementById('sortindex').value

if(firstletter==''){alert('indicate first letter!');
return false;
}
if (pickedOptions[i].substr(0,1)==firstletter){
newoption=document.createElement('option')
newoption.innerText=pickedOptions[i]
document.getElementById('alpha').appendChild(newoption)
}
}
}


</script>

</head>

<body onload="populateselect()">
<div id="popdata" style="display:none;">a aa abcd ezraz eraze tt ptpdppf pept lkj li lnnn lnlghllhg d f dfdf ffds sdfsd qdfqs qdq f qsdf s df sd
</div>
<select id="alpha">
<select/>
</select>
<input type="text" id="sortindex" MaxLength=1 />
<input type="button" onclick="startingwith()" />

</body>
</html>