PDA

Click to See Complete Forum and Search --> : Sorting an array


hpl
Jan 8th, 2003, 02:53 AM
I have a quit simple question. I have to drpdwnlsts, I wish to sort the content of drp2 by the way I choose in 1.
My code is:

<%@ Page Language="vb" %>
<script runat="server">


public Sub Page_Load()
if not page.isPostback then
Dim AnimalArray(4) As String
Dim strAnimal As String
AnimalArray(0) = "Dog"
AnimalArray(1) = "Cat"
AnimalArray(2) = "Elephant"
AnimalArray(3) = "Lion"
AnimalArray(4) = "Cat"

Dim SortArray(2) As String
Dim strSort As String
SortArray(0) = "Reverse"
SortArray(1) = "Forward"
SortArray(2) = "Alphabethical"

For Each strAnimal In AnimalArray
MyDropDownList.Items.Add(strAnimal)
Next

For Each strSort In SortArray
MyDropDownList2.Items.Add(strSort)
Next
end if
End Sub

public Sub SortAnimal(sender As Object, E As Eventargs)

Dim SortVal as string
sortval = MydropDownList2.selectedItem.Value
if sortval = "Reverse" then
'array.reverse(animalArray)
end if

end sub

</script>
<html>
<head>
<form id="Form1" method="post" runat="server">
<asp:dropdownlist id="MyDropDownList2" OnSelectedIndexChanged="sortAnimal" AutoPostBack="true" runat="server"></asp:dropdownlist>
<asp:dropdownlist id="MyDropDownList" runat="server"></asp:dropdownlist>
</form>
</head>
<body>
</body>
</html>

ChrisWise
Jan 13th, 2003, 08:15 AM
Hi,


Why not use instead of a string array the arraylist?

that way sorting is much easier:

Alphabetically:

arranimals.Sort()

Reverse:

arranimals.Reverse()


Im not sure what you mean by "Forward"

Hope it helps

Chris