|
||
|
Sorting Arrays Ascending or Descending
This is a sub-routine that can sort an array in ascending or descending order. It works by running the array,
then as a temporary variable is holding the value of that item, it loops through the array again an compares the
temporary value to all the other values in the array and reassigns the temporary value to the current array number
depending on of you are sorting it ascending or descending. You could compare it to a ladder.
The sub-routine will move the temporary value up or down the ladder accordingly and shift the other
values in the array to make room for it.
In the example below, I have two methods of declaring arrays and how to use them with this sub-routine.
Here is the ASP code for this script:
<% Sub SortArray(TheArr,AscDesc) TempVal = "" For x = 0 to UBound(TheArr) For y = x+1 to UBound(TheArr) If AscDesc = "Desc" Then If TheArr(x) < TheArr(y) then TempVal = TheArr(y) TheArr(y) = TheArr(x) TheArr(x) = TempVal End If Else If TheArr(x) > TheArr(y) then TempVal = TheArr(x) TheArr(x) = TheArr(y) TheArr(y) = TempVal End If End If Next Next End Sub 'Below are examples of how to use the SortArray sub Dim SomeArr(5) SomeArr(0) = "Triumph Motorcycles is also the oldest " & _ "continuous production motorcycle company in the world. " SomeArr(1) = "Gentlemen. You can't fight in here. " & _ "This is the War Room! " SomeArr(2) = "1981 Darrel Griffith, selected by the Jazz with the #2 overall pick in the NBA Draft" SomeArr(3) = "1979-87, 80 Kellen Winslow, San Diego Chargers Tight End" SomeArr(4) = "2003 Triumph Speedmaster" SomeArr(5) = "1972 Trident/BSA Rocket III" Response.Write "<div class=" & chr(34) & "breg" & chr(34) & " " & _ "style=" & chr(34) & "padding:5px;" & chr(34) & ">" & _ "<b>The Original Array</b>:" For x = 0 to UBound(SomeArr) Response.Write "<div class=" & chr(34) & "bnotes" & chr(34) & " " & _ "style=" & chr(34) & "padding-left:15px;" & chr(34) & ">" & _ "<b class=" & chr(34) & "Bull" & chr(34) & ">" & x & "</b>. " & _ SomeArr(x) & _ "</div>" Next Response.Write "</div>" Call SortArray(SomeArr,"Asc") Response.Write "<div class=" & chr(34) & "breg" & chr(34) & " " & _ "style=" & chr(34) & "padding:5px;" & chr(34) & ">" & _ "<b>The Array Sorted Ascending</b>:" For x = 0 to UBound(SomeArr) Response.Write "<div class=" & chr(34) & "bnotes" & chr(34) & " " & _ "style=" & chr(34) & "padding-left:15px;" & chr(34) & ">" & _ "<b class=" & chr(34) & "Bull" & chr(34) & ">" & x & "</b>. " & _ SomeArr(x) & _ "</div>" Next Response.Write "</div>" Call SortArray(SomeArr,"Desc") Response.Write "<div class=" & chr(34) & "breg" & chr(34) & " " & _ "style=" & chr(34) & "padding:5px;" & chr(34) & ">" & _ "<b>The Array Sorted Descending</b>:" For x = 0 to UBound(SomeArr) Response.Write "<div class=" & chr(34) & "bnotes" & chr(34) & " " & _ "style=" & chr(34) & "padding-left:15px;" & chr(34) & ">" & _ "<b class=" & chr(34) & "Bull" & chr(34) & ">" & x & "</b>. " & _ SomeArr(x) & _ "</div>" Next Response.Write "</div>" Response.Write "<hr>" ListItems = "Soren Winslow,Utah Jazz,Triumph Motorcycles,San Diego Chargers," & _ "ASP Scripts,CafePress Pages,Squidoo Pages,My Web Sites" ListArr = Split(ListItems, ",") Response.Write "<div class=" & chr(34) & "breg" & chr(34) & " " & _ "style=" & chr(34) & "padding:5px;" & chr(34) & ">" & _ "<b>The Original Array</b>:" For x = 0 to UBound(ListArr) Response.Write "<div class=" & chr(34) & "bnotes" & chr(34) & " " & _ "style=" & chr(34) & "padding-left:15px;" & chr(34) & ">" & _ "<b class=" & chr(34) & "Bull" & chr(34) & ">" & x & "</b>. " & _ ListArr(x) & _ "</div>" Next Response.Write "</div>" Call SortArray(ListArr,"Asc") Response.Write "<div class=" & chr(34) & "breg" & chr(34) & " " & _ "style=" & chr(34) & "padding:5px;" & chr(34) & ">" & _ "<b>The Array Sorted Ascending</b>:" For x = 0 to UBound(ListArr) Response.Write "<div class=" & chr(34) & "bnotes" & chr(34) & " " & _ "style=" & chr(34) & "padding-left:15px;" & chr(34) & ">" & _ "<b class=" & chr(34) & "Bull" & chr(34) & ">" & x & "</b>. " & _ ListArr(x) & _ "</div>" Next Response.Write "</div>" Call SortArray(ListArr,"Desc") Response.Write "<div class=" & chr(34) & "breg" & chr(34) & " " & _ "style=" & chr(34) & "padding:5px;" & chr(34) & ">" & _ "<b>The Array Sorted Descending</b>:" For x = 0 to UBound(ListArr) Response.Write "<div class=" & chr(34) & "bnotes" & chr(34) & " " & _ "style=" & chr(34) & "padding-left:15px;" & chr(34) & ">" & _ "<b class=" & chr(34) & "Bull" & chr(34) & ">" & x & "</b>. " & _ ListArr(x) & _ "</div>" Next Response.Write "</div>" Response.Write "<hr>" %>
Here are the results of the above ASP Script:
The Original Array: 0. Triumph Motorcycles is also the oldest continuous production motorcycle company in the world. 1. Gentlemen. You can't fight in here. This is the War Room! 2. 1981 Darrel Griffith, selected by the Jazz with the #2 overall pick in the NBA Draft 3. 1979-87, 80 Kellen Winslow, San Diego Chargers Tight End 4. 2003 Triumph Speedmaster 5. 1972 Trident/BSA Rocket III The Array Sorted Ascending: 0. 1972 Trident/BSA Rocket III 1. 1979-87, 80 Kellen Winslow, San Diego Chargers Tight End 2. 1981 Darrel Griffith, selected by the Jazz with the #2 overall pick in the NBA Draft 3. 2003 Triumph Speedmaster 4. Gentlemen. You can't fight in here. This is the War Room! 5. Triumph Motorcycles is also the oldest continuous production motorcycle company in the world. The Array Sorted Descending: 0. Triumph Motorcycles is also the oldest continuous production motorcycle company in the world. 1. Gentlemen. You can't fight in here. This is the War Room! 2. 2003 Triumph Speedmaster 3. 1981 Darrel Griffith, selected by the Jazz with the #2 overall pick in the NBA Draft 4. 1979-87, 80 Kellen Winslow, San Diego Chargers Tight End 5. 1972 Trident/BSA Rocket III The Original Array: 0. Soren Winslow 1. Utah Jazz 2. Triumph Motorcycles 3. San Diego Chargers 4. ASP Scripts 5. CafePress Pages 6. Squidoo Pages 7. My Web Sites The Array Sorted Ascending: 0. ASP Scripts 1. CafePress Pages 2. My Web Sites 3. San Diego Chargers 4. Soren Winslow 5. Squidoo Pages 6. Triumph Motorcycles 7. Utah Jazz The Array Sorted Descending: 0. Utah Jazz 1. Triumph Motorcycles 2. Squidoo Pages 3. Soren Winslow 4. San Diego Chargers 5. My Web Sites 6. CafePress Pages 7. ASP Scripts |