Results 1 to 2 of 2

Thread: Sorting an array

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049

    Question Sorting an array

    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:
    Code:
    <%@ 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>

  2. #2
    Junior Member
    Join Date
    Nov 2002
    Location
    Left past the postbox
    Posts
    30

    Reply

    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

Posting Permissions

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



Click Here to Expand Forum to Full Width