469,926 Members | 1,512 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,926 developers. It's quick & easy.

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 4908
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by jarkkotv | last post: by
1 post views Thread by bengamin | last post: by
16 posts views Thread by stojilcoviz | last post: by
48 posts views Thread by Alex Chudnovsky | last post: by
3 posts views Thread by djp | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.