I Need Help with sorting an arraylist (VB.net)

G

Guest

i have an arraylist that gets it's values dynamiclly from the database, after
paging, searching & more in the ASP.net file.
What im trying to do, is sort the results that are sorted in the ArrayList,
each time by another field.
The ArrayList contains a list of a class which i built, that fits these
results.
I was told to use a IComparer with the sort method, after i try'ed just
ArrayList1.Sort i got the following error:
System.ArgumentException: At least one object must implement IComparable

try'ed giving the Sort method an IComparable parameter, it still gives me
the same error.
How can i sort my ArrayList without building a full interface for it? (or is
it the only way?)

thanks in advance.
 
J

Jon Skeet [C# MVP]

eLisHa said:
i have an arraylist that gets it's values dynamiclly from the database, after
paging, searching & more in the ASP.net file.
What im trying to do, is sort the results that are sorted in the ArrayList,
each time by another field.
The ArrayList contains a list of a class which i built, that fits these
results.
I was told to use a IComparer with the sort method, after i try'ed just
ArrayList1.Sort i got the following error:
System.ArgumentException: At least one object must implement IComparable

try'ed giving the Sort method an IComparable parameter, it still gives me
the same error.
How can i sort my ArrayList without building a full interface for it? (or is
it the only way?)

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
G

Guest

Ok then... about that MSDN article, yeah, ive read it lots of times... it
never help me though.


My web page works like this:
Page, a search results page, gets search crietenia as a QuerySting
BindData Sub, which adds the needed data to the asp.net controls. It mainly gets the full tables from the database and searchs in them for the requested text, etc. When a result is found, it is added to my global ArrayList, as a Discount. example:

Public Class Discount
Private iID As Integer, iName As String, iBus As String
Sub New(sID As Integer, sName As String, sBus As String)
' This creats a new instance of the Discount class, gets all information
needed about the discount.
' for instance:
Me.iID = sID
Me.iName = sName
Me.iBus = sBus
End Sub

Public ReadOnly Property ID() As String
Get
Return iID
End Get
End Property


Public ReadOnly Property Name() As String
Get
Return iName
End Get
End Property


Public ReadOnly Property Bus() As String
Get
Return iBus
End Get
End Property



End Class
To add a discount to the global ArrayList, i use the Add method:
ArrayList1.Add(New Discount(drDiscount("id"), drDiscount("type_name"),
("Business")))
After the BindData() Finishes, there is a function for paging, which gets it pageId value from ViewState.
My client wants me to build a sort method for the results. ofcourse it has
to run before the paging one, so the paging will be right.

So what i need help in, is building that Sort method, which will work on ANY
of the currect discount property's.

I hope my question is easier to understand now.
 
J

Jon Skeet [C# MVP]

eLisHa said:
So what i need help in, is building that Sort method, which will work on ANY
of the currect discount property's.

Well, it would probably be best to have a separate implementation of
IComparer for each property - e.g. DiscountIDComparer,
DiscountNameComparer etc. Implement IComparer, create an instance of
the implementation, then pass that to ArrayList.Sort.
 
G

Guest

Thank you for replying... i was wondering, could you write a small example
for me on how to create this sort method related to the new IComparer which i
need to build for each property (could use help there too ;))

Again, Thank you!
 
J

Jon Skeet [C# MVP]

eLisHa said:
Thank you for replying... i was wondering, could you write a small example
for me on how to create this sort method related to the new IComparer which i
need to build for each property (could use help there too ;))

You don't need to create the sort method. Suppose you have a variable
"list" which is an ArrayList. You'd just do:

IComparer foo = new DiscountIDComparer(); // For instance
list.Sort(foo);
 
G

Guest

Hmm, i think i got it.... except the

IComparer foo = new DiscountIDComparer()

What is DiscountIDComparer? I need to create it for each property? What type
/ value it has?

Thanks again.
 
J

Jon Skeet [C# MVP]

eLisHa said:
Hmm, i think i got it.... except the

IComparer foo = new DiscountIDComparer()

What is DiscountIDComparer? I need to create it for each property? What type
/ value it has?

As I said before, it's a type which implements IComparer. It would
compare two any discounts by ID. You'd have a different one for each
property.

You *could* do it with one type, either with a big switch statement
(nasty) or reflection (not type safe, relatively slow) but IMO the
simplest thing would be to have a separate type for each property you
need to sort by.
 
G

Guest

Thanks again for replying... but i still dont get the point.
I have this class - Discount, which exists in my ArrayList dynamic times.
I want to sort the ArrayList, so i understood from you that i need to create
a IComparer to do that, an IComparer for each field to sort by.

Now - i dont understand how to do that - creating the IComparer for sorting
by.
I looked in .net sdk, and IComparer doesnt have a constructor (v1.1). It
just has one member, a method - Compare.

How do i create a new instance of it (for instance - DiscountIDComparer),
that will know to compare the DiscountID's?

Thank you so much.
 
J

Jon Skeet [C# MVP]

eLisHa said:
Thanks again for replying... but i still dont get the point.
I have this class - Discount, which exists in my ArrayList dynamic times.
I want to sort the ArrayList, so i understood from you that i need to create
a IComparer to do that, an IComparer for each field to sort by.
Yes.

Now - i dont understand how to do that - creating the IComparer for sorting
by.
I looked in .net sdk, and IComparer doesnt have a constructor (v1.1). It
just has one member, a method - Compare.

How do i create a new instance of it (for instance - DiscountIDComparer),
that will know to compare the DiscountID's?

It's an interface. You need to implement the interface yourself:

public class DiscountIDComparer : IComparer
{
public int Compare (object first, object second)
{
// Compare first and second - they should both
// be references to Discount instances.
// See the docs for more information
}
}

If you haven't used interfaces before, now would be a very good time to
put your project down temporarily and learn about them before you
continue. They're very, very important.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top