473,811 Members | 3,610 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ArrayList Sorting in VB.NET - How to use ICompare

Hello,

I have a basic understanding of Objects, inheritance, polymorhpism, etc. I
am new to VB.NET and I am having a problem figuring this out.

I want to sort a CheckBoxList. Using the ArrayList seemed to make the most
sense. When I use it I get the error: At least one object must implement
IComparable. Here's the code that produces that error:

Public Class Sorting
Implements IComparable, IComparer

Private Shared aryOrigData As New ArrayList
Private Shared aryChangedData As New ArrayList
Public Shared Function GatherChangedCh eckBoxData(ByVa l chkboxlist As
CheckBoxList)
Dim li As ListItem

For Each li In chkboxlist.Item s
aryChangedData. Add(li)
Next
aryChangedData. Sort()

End Function
The above code is for a web form. As I am learning to do this, I made
borrowed the code from the Help files and placed it into a new project, I
didn't get any error. Here's that code:

Imports System
Imports System.Collecti ons
Imports Microsoft.Visua lBasic
Public Class SamplesArrayLis t

Public Shared Sub Main()
' Creates and initializes a new ArrayList.
Dim myAL As New ArrayList
myAL.Add("The")
myAL.Add("quick ")
myAL.Add("brown ")
myAL.Add("fox")
myAL.Add("jumps ")
myAL.Add("over" )
myAL.Add("the")
myAL.Add("lazy" )
myAL.Add("dog")

' Displays the values of the ArrayList.
Console.WriteLi ne("The ArrayList initially contains the " _
+ "following values:")
PrintIndexAndVa lues(myAL)

' Sorts the values of the ArrayList.
myAL.Sort()

' Displays the values of the ArrayList.
Console.WriteLi ne("After sorting:")
PrintIndexAndVa lues(myAL)
End Sub 'Main
Public Shared Sub PrintIndexAndVa lues(ByVal myList As IEnumerable)
Dim i As Integer = 0
Dim myEnumerator As System.Collecti ons.IEnumerator = _
myList.GetEnume rator()
While myEnumerator.Mo veNext()
Console.WriteLi ne(ControlChars .Tab + "[{0}]:" + ControlChars.Ta b
_
+ "{1}", i, myEnumerator.Cu rrent)
i = i + 1
End While
Console.WriteLi ne()
End Sub
End Class

=============== ==============
My question is then, what is the difference between the two? Is it becuase
one is using ListItems? If that is the case how do I solve it? If it is
implementing ICompare, could someone direct me to a good source that
explains it well.

Thanks so much for your help.

Nov 18 '05 #1
2 5076
Try using a System.Collecti ons.SortedList instead.

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Rob G" <gu*******@hotm ail.com> wrote in message
news:10******** *****@corp.supe rnews.com...
Hello,

I have a basic understanding of Objects, inheritance, polymorhpism, etc. I
am new to VB.NET and I am having a problem figuring this out.

I want to sort a CheckBoxList. Using the ArrayList seemed to make the most
sense. When I use it I get the error: At least one object must implement
IComparable. Here's the code that produces that error:

Public Class Sorting
Implements IComparable, IComparer

Private Shared aryOrigData As New ArrayList
Private Shared aryChangedData As New ArrayList
Public Shared Function GatherChangedCh eckBoxData(ByVa l chkboxlist As
CheckBoxList)
Dim li As ListItem

For Each li In chkboxlist.Item s
aryChangedData. Add(li)
Next
aryChangedData. Sort()

End Function
The above code is for a web form. As I am learning to do this, I made
borrowed the code from the Help files and placed it into a new project, I
didn't get any error. Here's that code:

Imports System
Imports System.Collecti ons
Imports Microsoft.Visua lBasic
Public Class SamplesArrayLis t

Public Shared Sub Main()
' Creates and initializes a new ArrayList.
Dim myAL As New ArrayList
myAL.Add("The")
myAL.Add("quick ")
myAL.Add("brown ")
myAL.Add("fox")
myAL.Add("jumps ")
myAL.Add("over" )
myAL.Add("the")
myAL.Add("lazy" )
myAL.Add("dog")

' Displays the values of the ArrayList.
Console.WriteLi ne("The ArrayList initially contains the " _
+ "following values:")
PrintIndexAndVa lues(myAL)

' Sorts the values of the ArrayList.
myAL.Sort()

' Displays the values of the ArrayList.
Console.WriteLi ne("After sorting:")
PrintIndexAndVa lues(myAL)
End Sub 'Main
Public Shared Sub PrintIndexAndVa lues(ByVal myList As IEnumerable)
Dim i As Integer = 0
Dim myEnumerator As System.Collecti ons.IEnumerator = _
myList.GetEnume rator()
While myEnumerator.Mo veNext()
Console.WriteLi ne(ControlChars .Tab + "[{0}]:" + ControlChars.Ta b _
+ "{1}", i, myEnumerator.Cu rrent)
i = i + 1
End While
Console.WriteLi ne()
End Sub
End Class

=============== ==============
My question is then, what is the difference between the two? Is it becuase
one is using ListItems? If that is the case how do I solve it? If it is
implementing ICompare, could someone direct me to a good source that
explains it well.

Thanks so much for your help.

Nov 18 '05 #2
Thanks for the tip. I'll look into it.

"Kevin Spencer" <ks******@takem pis.com> wrote in message
news:OY******** *****@TK2MSFTNG P10.phx.gbl...
Try using a System.Collecti ons.SortedList instead.

--
HTH,
Kevin Spencer
.Net Developer
Microsoft MVP
Big things are made up
of lots of little things.

"Rob G" <gu*******@hotm ail.com> wrote in message
news:10******** *****@corp.supe rnews.com...
Hello,

I have a basic understanding of Objects, inheritance, polymorhpism, etc. I am new to VB.NET and I am having a problem figuring this out.

I want to sort a CheckBoxList. Using the ArrayList seemed to make the most sense. When I use it I get the error: At least one object must implement
IComparable. Here's the code that produces that error:

Public Class Sorting
Implements IComparable, IComparer

Private Shared aryOrigData As New ArrayList
Private Shared aryChangedData As New ArrayList
Public Shared Function GatherChangedCh eckBoxData(ByVa l chkboxlist As
CheckBoxList)
Dim li As ListItem

For Each li In chkboxlist.Item s
aryChangedData. Add(li)
Next
aryChangedData. Sort()

End Function
The above code is for a web form. As I am learning to do this, I made
borrowed the code from the Help files and placed it into a new project, I didn't get any error. Here's that code:

Imports System
Imports System.Collecti ons
Imports Microsoft.Visua lBasic
Public Class SamplesArrayLis t

Public Shared Sub Main()
' Creates and initializes a new ArrayList.
Dim myAL As New ArrayList
myAL.Add("The")
myAL.Add("quick ")
myAL.Add("brown ")
myAL.Add("fox")
myAL.Add("jumps ")
myAL.Add("over" )
myAL.Add("the")
myAL.Add("lazy" )
myAL.Add("dog")

' Displays the values of the ArrayList.
Console.WriteLi ne("The ArrayList initially contains the " _
+ "following values:")
PrintIndexAndVa lues(myAL)

' Sorts the values of the ArrayList.
myAL.Sort()

' Displays the values of the ArrayList.
Console.WriteLi ne("After sorting:")
PrintIndexAndVa lues(myAL)
End Sub 'Main
Public Shared Sub PrintIndexAndVa lues(ByVal myList As IEnumerable)
Dim i As Integer = 0
Dim myEnumerator As System.Collecti ons.IEnumerator = _
myList.GetEnume rator()
While myEnumerator.Mo veNext()
Console.WriteLi ne(ControlChars .Tab + "[{0}]:" +

ControlChars.Ta b
_
+ "{1}", i, myEnumerator.Cu rrent)
i = i + 1
End While
Console.WriteLi ne()
End Sub
End Class

=============== ==============
My question is then, what is the difference between the two? Is it becuase one is using ListItems? If that is the case how do I solve it? If it is
implementing ICompare, could someone direct me to a good source that
explains it well.

Thanks so much for your help.


Nov 18 '05 #3

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

Similar topics

4
1836
by: jarkkotv | last post by:
Hi everyone! I'm having a little problem when sorting the ArrayList and I was wondering if there is a .NET guru who can help me out :) I'm trying to sort ArrayList alphabetically in ASP.Net -page using the ArrayList.Sort(), but I not getting the results sorted properly. I'm having problems especially with the scandic characters although CurrentCulture is set to Finnish, which should handle these charcters
2
7194
by: Clinton Pierce | last post by:
I've filled a DataTable with columns that have custom type (a class that I'm using to keep track of other things, not just a value). When the .Select method goes to sort this column, how do I let .Net know what value I want used for the sort? Sometimes it's going to be sorted as a decimal and other times a string. I don't really know which until runtime. Thank you.
1
29881
by: bengamin | last post by:
Hi, I declare two ArrayList variables.How can I compare if the two ArrayList are Value Equal. Thanks! Ben
16
2033
by: stojilcoviz | last post by:
I've a datagrid whose datasource is an arraylist object. The arraylist holds many instances of a specific class. I've two questions about this: 1 - Is there a way by which I can obtain a reference to the arraylist item the current row points to? 2 - Is it possible to sort the grid? Many thanks in advance.
16
6434
by: RCS | last post by:
So I have an ArrayList that gets populated with objects like: myAL.Add(new CustomObject(parm1,parm2)); I'm consuming this ArrayList from an ObjectDataSource and would like to have this support sorting (because it's ultimately being consumed in a GridView). I can't simply sort the ArrayList (because it only knows it's holding a bunch of objects). So I need a way to sort the ArrayList, based on the data - that is within the objects that...
6
1828
by: AAJ | last post by:
Hi all I have some objects that I add to an ArrayList. Each object is from the same class and has methods etc. Each can be identified by a unique property, in this case PK e.g. PK =1 for Instance1.PK PK =2 for Instance2.PK
48
4495
by: Alex Chudnovsky | last post by:
I have come across with what appears to be a significant performance bug in ..NET 2.0 ArrayList.Sort method when compared with Array.Sort on the same data. Same data on the same CPU gets sorted a lot faster with both methods using .NET 1.1, that's why I am pretty sure its a (rather serious) bug. Below you can find C# test case that should allow you to reproduce this error, to run it you will need to put 2 data files into current directory...
2
2137
by: MikeY | last post by:
Hi everyone, After reading various posts I'm still scratching my head and unsure of what approach to take. I have created buttons that upon clicking, the buttons add an item name (myName) to my ListView (lvBody) and at the same time, also add subitems(unitPrice); The problems is that if I don't have sort going on, then all "subitems" are fine. If I have sorting, then the "subitems" that are being placed above the last Alpha "subitems"...
3
3129
by: djp | last post by:
Hi I have to sort arraylist. I tried to do this using this page as a reference: http://www.java2s.com/Code/CSharp/Collections-Data-Structure/UseIComparer.htm I did it exactly the same way but unfortunately in opposite to the example from this link here it does not sort. I Create Nodes ( color, name ) and add them to nodeList in Graph class. Then i try to sort.. The code is relatively simple:
0
9604
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
10644
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
10379
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10394
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10127
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...
0
9201
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5552
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
5690
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3015
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.