Sorting an Array..?

1. Senior Member
Join Date
Dec 1969
Posts
719

## Sorting an Array..?

Is it possible to sort an array by the values stored in it...<BR><BR>I have an array made up like...<BR><BR>ary(0) = "Fuehner, Jason"<BR>ary(1) = "Smith, John"<BR>ary(2) = "Doe, Jane"<BR><BR><BR>Is it possible to sort it so that it&#039;s in alpha order...<BR><BR>Any help would be great..!

2. Senior Member
Join Date
Dec 1969
Posts
2,225

## See this FAQ

http://www.aspfaqs.com/aspfaqs/ShowFAQ.asp?FAQID=83

3. Senior Member
Join Date
Dec 1969
Posts
719

## RE: See this FAQ

Chris...<BR><BR>I might be a bit wacked but...<BR><BR>If I use the bubble sort from the FAQ how do I make it sort ascending by the value stored in the array using what&#039;s below..?<BR><BR>for x = 0 to ubound(aryAuthors) - 1<BR> for y = 0 to x<BR> if aryAuthors(y) &#062; aryAuthors(y + 1) then<BR> temp = aryAuthors(y + 1)<BR> aryAuthors(y + 1) = aryAuthors(y)<BR> aryAuthors(y) = temp <BR> end if<BR> next<BR>next

4. Senior Member
Join Date
Dec 1969
Posts
96,118

## That code is *WRONG*!

The first line should read:<BR><BR>For x = UBound(aryAuthors)-1 To 0 Step -1<BR><BR>And to change the ordering, just substitute &#060; for &#062; in the IF line.<BR><BR>

5. Senior Member
Join Date
Dec 1969
Posts
96,118

## I just fixed the FAQ...

Since Richard was only using that code to compare the performance vs. his JS sorting scheme, he obviously never tested it to find out if it actually worked!<BR><BR>I&#039;m pretty sure it&#039;s right now. Tell me if you find a bug.<BR><BR>

6. Senior Member
Join Date
Dec 1969
Posts
719

## RE: That code is *WRONG*!

what do you mean substitue for in the IF line..?

7. Senior Member
Join Date
Dec 1969
Posts
96,118

## I mean...

...change the character &#062; to the character &#060;<BR><BR>What else would make sense? You said you wanted to change it from sorting smallest first to sorting largest first, yes? So change the "order" of the comparison. That&#039;s all.<BR><BR>for x = [hl="yellow"]UBound(aryAuthors)-1 To 0 Step -1[/hl]<BR> for y = 0 to x <BR> if aryAuthors(y)[hl="yellow"] &#060; [/hl]aryAuthors(y + 1) then <BR> temp = aryAuthors(y + 1) <BR> aryAuthors(y + 1) = aryAuthors(y) <BR> aryAuthors(y) = temp <BR> end if <BR> next <BR>next

8. Senior Member
Join Date
Dec 1969
Posts
719

## RE: I mean...

Okay... When I have it as "&#062;" I get...<BR><BR>DRANE, JAMES F.<BR>DRANE, JAMES F.<BR>CAMPBELL, COURTNEY<BR>CAMPBELL, COURTNEY<BR><BR>If I change it the other way I get...<BR><BR>FUEHNER, JASON<BR>DRANE, JAMES F.<BR>CAMPBELL, COURTNEY<BR>CAMPBELL, COURTNEY<BR><BR>As you can see for some reason I&#039;m for one not getting the same amount of values returned and for two their not sorting in ascending order by the first letter of the last name...<BR><BR>Anyone have any ideas...

9. Senior Member
Join Date
Dec 1969
Posts
96,118

## It worked perfectly...

...in my admittedly simple test case (only ten names).<BR><BR>Are you showing us the *true* case of those names (upper case/lower case)?<BR><BR>How about dumping *all* the names *prior to* AND *after* the sort and showing us what they are?<BR><BR>That is do this:<BR><BR>&#060;%<BR>...<BR>Response.Write "BEFORE:&lt;BR&#062;" & Join(aryAuthors,"&lt;BR&#062;") & "&#060;HR&#062;" & vbNewLine<BR>... do the sort ...<BR>Response.Write "AFTER:&lt;BR&#062;" & Join(aryAuthors,"&lt;BR&#062;") & "&#060;HR&#062;" & vbNewLine<BR>...<BR>%&#062;<BR><BR>Show us what that gives you. Copy/Paste.<BR><BR>

10. Senior Member
Join Date
Dec 1969
Posts
719

## RE: It worked perfectly...

When I iterate through the array the first before the sort I get...<BR><BR>BEFORE: DRANE, JAMES F.<BR>BEFORE: FUEHNER, JASON<BR>BEFORE: CAMPBELL, COURTNEY<BR>BEFORE: CAMPBELL, COURTNEY<BR><BR>After I get...<BR><BR>AFTER: CAMPBELL, COURTNEY<BR>AFTER: CAMPBELL, COURTNEY<BR>AFTER: CAMPBELL, COURTNEY<BR>AFTER: DRANE, JAMES F.<BR><BR><BR>The string that I&#039;m splitting into an unbound array looks like...<BR><BR>STRING: <BR><BR>DRANE, JAMES F. &#124 FUEHNER, JASON &#124 CAMPBELL, COURTNEY &#124 CAMPBELL, COURTNEY &#124

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•