473,849 Members | 1,851 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.Argument Exception: 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 8089
eLisHa <eL****@discuss ions.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.Argument Exception: 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.co m>
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(drDisc ount("id"), drDiscount("typ e_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****@discuss ions.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. DiscountIDCompa rer,
DiscountNameCom parer etc. Implement IComparer, create an instance of
the implementation, then pass that to ArrayList.Sort.

--
Jon Skeet - <sk***@pobox.co m>
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****@discuss ions.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. DiscountIDCompa rer,
DiscountNameCom parer etc. Implement IComparer, create an instance of
the implementation, then pass that to ArrayList.Sort.

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

Sep 10 '05 #6
eLisHa <eL****@discuss ions.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 DiscountIDCompa rer(); // For instance
list.Sort(foo);

--
Jon Skeet - <sk***@pobox.co m>
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 DiscountIDCompa rer()

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

Thanks again.

Sep 11 '05 #8
eLisHa <eL****@discuss ions.microsoft. com> wrote:
Hmm, i think i got it.... except the

IComparer foo = new DiscountIDCompa rer()

What is DiscountIDCompa rer? 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.co m>
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 - DiscountIDCompa rer),
that will know to compare the DiscountID's?

Thank you so much.
Sep 12 '05 #10

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

Similar topics

3
4384
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 that it is. If I am not mistaken, I need to install ASP.NET 1.1 Is this correct? If so, will all of my other sites that use ASP still work the same, or will they have to be rebuilt?
2
1792
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 programming experience in Lotus Notes and I have developped several 'basic' websites, but .NET and C# are totally new to me. I wonder if it is realistic to take the examen without actually have practical experience with Visual C#.NET.
21
2045
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 sure how to call my native C++ functions from my C++ DLLs. The confusion is this: I currently have Visual Studio 2003 it is not clear whether I should use managed extensions or C++/Interop?
2
1062
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 found that I cannot run ASP.net formated pages on my server the way that it is. If I am not mistaken, I need to install ASP.NET 1.1
4
1986
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, which could be why no-one answered it!! I've now narrowed the problem down, so am starting a new, more specific, thread. Sorry it's a bit long, but it's reasonably simple stuff. I have a page where I'm showing product details, including any
3
2392
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 coordinates. The algorithm that I am trying to implement says that the resulting data structure is a multi-pointer list ((d-1)tuply threaded list), each node of which is threaded in each of the single pointer lists corresponding to the chosen...
2
2462
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 values from. For example, the XML file is meant to return the values from node"config2", because the "configoption" node's value is that. I'm extremely new to XML and the code that accesses it, but from what I could gather from materials read, I got...
0
3143
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 ' Append one or more fields... Call rs.Fields.Append("Number", adInteger)
4
1595
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
2435
by: Satish Yeruva | last post by:
How do i find performance difference between sorting ArrayList of Integers and ArrayList of Strings?
0
9893
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9738
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11005
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10349
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7894
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5735
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5921
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
4139
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3177
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.