0
 Dim alCustomers as New ArrayList

 Dim li1 As New ListItem("John", 7)
 alCustomers.Add(li1)

 Dim li2 As New ListItem("Abe", 2)
 alCustomers.Add(li2)

How can I sort the alCustomers arraylist, by Value?

Abe,2

John,7

2
  • What version of .Net are you using? Commented Feb 20, 2013 at 16:18
  • Hi, using .net 2.0 unfortunately, the answers look great though but the OrderBy function was not in .net 2.0. Hope there is a way around this Commented Feb 21, 2013 at 6:41

2 Answers 2

1

In C# with .NET 3.5 or newer it would work like this:

// Create a list of ListItem objects
List<ListItem> alCustomers = new List<ListItem>();

// Add the list items
alCustomers.Add(new ListItem("John", 7));
alCustomers.Add(new ListItem("Abe", 2));

var orderedCustomers = alCustomers
    // Order the items by their value...
    .OrderBy(item => item.Value)
    // and convert it to a list.
    .ToList();

Unfortunately since the last version of Visual Basic I used was VB6, I am not sure how to translate that. This is my best guess:

// Create a list of ListItem objects
Dim alCustomers as New List(Of ListItem)

// Add the list items
alCustomers.Add(New ListItem("John", 7))
alCustomers.Add(New ListItem("Abe", 2))

Dim orderedCustomers As List(Of ListItem) = alCustomers
    // Order the items by their value...
    .OrderBy(Function(item As ListItem) item.Value)
    // and convert it to a list.
    .ToList(Of ListItem)()
Sign up to request clarification or add additional context in comments.

Comments

1

Using Linq:

alCustomers = alCustomers.OrderBy(Function(item) item.Value)

This solution requires a List instead of an ArrayList.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.