How to sort an arraylist in VB.NET

Posted on 2005-02-25
Medium Priority
Last Modified: 2010-08-05

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.


I want to sort the
Question by:vmandem
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

Wim_Bl earned 140 total points
ID: 13403251

check this out:

In addition, maybe you should make a class for each element in your arraylist (containing the members type, id, etc...).
Next, you can add functionality to this class to sort it the way you want, by using an interface IComparer. This will allow
you to sort on the field you want, the way you want. In this case, maybe the next link can help:

LVL 41

Expert Comment

ID: 13405750
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


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.

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Microsoft Reports are based on a report definition, which is an XML file that describes data and layout for the report, with a different extension. You can create a client-side report definition language (*.rdlc) file with Visual Studio, and build g…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question