Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


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

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

Technology Partners: 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

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
1.0 - Introduction Converting Visual Basic 6.0 (VB6) to Visual Basic 2008+ (VB.NET). If ever there was a subject full of murkiness and bad decisions, it is this one!   The first problem seems to be that people considering this task of converting…
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Enter Foreign and Special Characters Enter characters you can't find on a keyboard using its ASCII code ... and learn how to make a handy reference for yourself using Excel ~ Use these codes in any Windows application! ... whether it is a Micr…

572 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