473,499 Members | 1,593 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 GatherChangedCheckBoxData(ByVal chkboxlist As
CheckBoxList)
Dim li As ListItem

For Each li In chkboxlist.Items
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.Collections
Imports Microsoft.VisualBasic
Public Class SamplesArrayList

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.WriteLine("The ArrayList initially contains the " _
+ "following values:")
PrintIndexAndValues(myAL)

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

' Displays the values of the ArrayList.
Console.WriteLine("After sorting:")
PrintIndexAndValues(myAL)
End Sub 'Main
Public Shared Sub PrintIndexAndValues(ByVal myList As IEnumerable)
Dim i As Integer = 0
Dim myEnumerator As System.Collections.IEnumerator = _
myList.GetEnumerator()
While myEnumerator.MoveNext()
Console.WriteLine(ControlChars.Tab + "[{0}]:" + ControlChars.Tab
_
+ "{1}", i, myEnumerator.Current)
i = i + 1
End While
Console.WriteLine()
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 5059
Try using a System.Collections.SortedList instead.

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

"Rob G" <gu*******@hotmail.com> wrote in message
news:10*************@corp.supernews.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 GatherChangedCheckBoxData(ByVal chkboxlist As
CheckBoxList)
Dim li As ListItem

For Each li In chkboxlist.Items
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.Collections
Imports Microsoft.VisualBasic
Public Class SamplesArrayList

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.WriteLine("The ArrayList initially contains the " _
+ "following values:")
PrintIndexAndValues(myAL)

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

' Displays the values of the ArrayList.
Console.WriteLine("After sorting:")
PrintIndexAndValues(myAL)
End Sub 'Main
Public Shared Sub PrintIndexAndValues(ByVal myList As IEnumerable)
Dim i As Integer = 0
Dim myEnumerator As System.Collections.IEnumerator = _
myList.GetEnumerator()
While myEnumerator.MoveNext()
Console.WriteLine(ControlChars.Tab + "[{0}]:" + ControlChars.Tab _
+ "{1}", i, myEnumerator.Current)
i = i + 1
End While
Console.WriteLine()
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******@takempis.com> wrote in message
news:OY*************@TK2MSFTNGP10.phx.gbl...
Try using a System.Collections.SortedList instead.

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

"Rob G" <gu*******@hotmail.com> wrote in message
news:10*************@corp.supernews.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 GatherChangedCheckBoxData(ByVal chkboxlist As
CheckBoxList)
Dim li As ListItem

For Each li In chkboxlist.Items
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.Collections
Imports Microsoft.VisualBasic
Public Class SamplesArrayList

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.WriteLine("The ArrayList initially contains the " _
+ "following values:")
PrintIndexAndValues(myAL)

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

' Displays the values of the ArrayList.
Console.WriteLine("After sorting:")
PrintIndexAndValues(myAL)
End Sub 'Main
Public Shared Sub PrintIndexAndValues(ByVal myList As IEnumerable)
Dim i As Integer = 0
Dim myEnumerator As System.Collections.IEnumerator = _
myList.GetEnumerator()
While myEnumerator.MoveNext()
Console.WriteLine(ControlChars.Tab + "[{0}]:" +

ControlChars.Tab
_
+ "{1}", i, myEnumerator.Current)
i = i + 1
End While
Console.WriteLine()
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
1813
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...
2
7168
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...
1
29818
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
2003
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...
16
6395
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...
6
1815
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...
48
4406
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...
2
2106
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...
3
3115
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...
0
7132
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
7178
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
7223
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...
1
6899
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...
0
7390
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...
1
4919
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...
0
3103
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...
0
3094
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
665
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.