473,411 Members | 1,997 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

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

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.
Sep 10 '05 #1
10 8041
eLisHa <eL****@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Sep 10 '05 #2
eLisHa,

You know that at Arraylist sort on MSDN is a complete sample that in my
opinion describes your question.

http://msdn.microsoft.com/library/de...ssorttopic.asp

I hope this helps,

Cor
Sep 10 '05 #3
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.
Sep 10 '05 #4
eLisHa <eL****@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Sep 10 '05 #5
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!

"Jon Skeet [C# MVP]" wrote:
eLisHa <eL****@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Sep 10 '05 #6
eLisHa <eL****@discussions.microsoft.com> wrote:
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);

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Sep 10 '05 #7
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.

Sep 11 '05 #8
eLisHa <eL****@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Sep 11 '05 #9
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.
Sep 12 '05 #10
eLisHa <eL****@discussions.microsoft.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Sep 12 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Mike | last post by:
I have a win2K server that runs my web sites. I need to use ASP.net on one of my sites. I am just learning about ASP.net and found that I cannot run ASP.net formated pages on my server the way...
2
by: Tina | last post by:
Hello, I'm currently preparing for the exam 70-315. I'm using the Self-paced training kit "Developing Web Applications with Microsoft Visual C#.NET", but I find it rather difficult. I have...
21
by: Paul Tremblay | last post by:
Hi All, I am a veteran C/C++ programmer (Unix background) and I want to get to speed with Visual Studio .Net I have legacy C/C++ code that I want to use in my application. However, I'm not...
2
by: Mike | last post by:
I posted this to a general asp newsgroup and was referred here..... I have a win2K server that runs my web sites. I need to use ASP.net on one of my sites. I am just learning about ASP.net and...
4
by: Alan Silver | last post by:
Hello, I'm trying to use an ArrayList to do data binding, but am getting an error I don't understand. I posted this in another thread, but that was all confused with various other problems,...
3
by: acosgaya | last post by:
Hi, I would like some help as how to approach the following problem: I have a set of d-dimensional points (e.g (3,5,8,9,5) is a 5-dimensional point), and I need to sort the points on each of the...
2
by: XML Beginner | last post by:
I have an XML file that contains values that my application needs, so it knows which database to connect to. It also contains a configuration option so that I can specify which node to return...
0
by: Yarik | last post by:
Hello, Here is a sample (and very simple) code that binds an Access 2003 form to a fabricated ADO recordset: ' Create recordset... Dim rs As ADODB.Recordset: Set rs = New ADODB.Recordset '...
4
by: psycho | last post by:
i was trying to bind a repeater with an arraylist. what i was not able to figure out was what should i use in the Eval("") expression. Any suggestions .
1
by: Satish Yeruva | last post by:
How do i find performance difference between sorting ArrayList of Integers and ArrayList of Strings?
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.