472,328 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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 4997
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
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...
2
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...
1
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
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:...
16
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...
6
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...
48
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...
2
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...
3
by: djp | last post by:
Hi I have to sort arraylist. I tried to do this using this page as a reference:...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

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.