Link to home
Start Free TrialLog in
Avatar of vmandem
vmandem

asked on

How to sort an arraylist in VB.NET

Hi

I'm getting data from the datareader and I'm adding all the data to arraylist but I want to sort the arraylist based on a particular column. My code looks like this:

    Dim m_alData As New ArrayList
If DR.HasRows Then
            While DR.Read()
                ' m_alData.Add(DR("xp_type_cd"))
                m_alData.Add(DR(0)) '->Type
                m_alData.Add(DR(1)) '->ID
                m_alData.Add(DR(2)) '->Trans Name -->Required
            End While

End if

I want to sort the m_alData by 'Trans Name '. How do i do that.

Thanks
vm

I want to sort the
ASKER CERTIFIED SOLUTION
Avatar of Wim_Bl
Wim_Bl
Flag of Belgium image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hold on... let's talk about what you're trying to do.

For most folks, if they plan to do a lot of manipulation of rows and columns of data, they tend to put that data in a DataTable object.  That way you can sort, query, do math, etc on rows and columns.   Using a DataTable also allows you to bind to other windows controls (like a DataGrid).

So, the idea of taking data from a database... putting it into an arraylist... then trying to manipulate the columns in arraylist, seems like the long way around.   Instead you should consider putting the data in a DataTable with a single line of code

            DataAdapter.Fill(DataTable)

Now,  you can sort the data by any column with a DataView object or by issuing a Rows.Select command.

If you really really really need to put the data in an array, I'd consider a multi-dimensional array so that an entire row of data would fit in the "row" of the array.