By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,417 Members | 1,088 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,417 IT Pros & Developers. It's quick & easy.

Sort an Arraylist

P: n/a
I need to sort an Arraylist. The Arraylist is contained in classArt. The
Arraylist contains objects that have been defined in a sperate class, objArt.
I have attempted to implement the sort using IComparable, but I'm hopelessly
lost. With the addition of "Implements ICompariable" and the "CompareTo"
function the program will ont even compile.
Any suggestion that you have would be gratefully welcomed. I am
including the relevant code:

Public Class classArt
Implements IComparable

Private Shared ArtList As New ArrayList
Private ArtIndex As Integer
Private ArtFileName As String = "art.xml"

Public Sub SaveFile() 'save ArtList to ArtFile
Dim Msg1 As String = "ArtFile was not saved! Reason: "
Dim Msg2 As String = "Art"
Dim sf As New SoapFormatter
Dim fs As New FileStream(ArtFileName, FileMode.Create)
Try
sf.Serialize(fs, ArtList)
Catch ex As Exception
MsgBox(Msg1 & ex.Message, MsgBoxStyle.Critical, Msg2)
Finally
fs.Close()
End Try
End Sub

Public Sub GetFile() 'get ArtList from ArtFile
Dim Msg1 As String = "Failed to open ArtList! Reason: "
Dim Msg2 As String = "Art"
If System.IO.File.Exists(ArtFileName) Then
Dim fs As New FileStream(ArtFileName, FileMode.Open)
Dim sf As New SoapFormatter
Try
ArtList = CType(sf.Deserialize(fs), ArrayList)
Catch ex As Exception
MsgBox(Msg1 & ex.Message, MsgBoxStyle.Critical, Msg2)
Finally
fs.Close()
sf = Nothing
fs = Nothing
End Try
Else
AddArt()
End If
End Sub

Public Function CompareTo(ByVal xArt As objArt) As Integer Implements
System.IComparable.CompareTo
Dim xDes As String
xDes = xArt.Des
Return "hi".CompareTo(xDes)
End Function

Public Sub AddArt()
'add an empty objArt to ArtList
Dim emptyArt As New objArt
With emptyArt
.Des = ""
.Title = ""
.Pic = ""
.Type = ""
.Room = ""
.Location = ""
.Height = 0
.Width = 0
.Depth = 0
.Bequeth = ""
.Appraised = ""
.Value = 0
End With
ArtList.Add(emptyArt)
emptyArt = Nothing
End Sub

End Class

<[Serializable]()> Public Class objArt
'Implements IComparable

Public Title As String
Public Des As String
Public Pic As String
Public Type As String
Public Room As String
Public Location As String
Public Height As Integer
Public Width As Integer
Public Depth As Integer
Public Bequeth As String
Public Appraised As String
Public Value As Integer

End Class
--
GrandpaB
Nov 21 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
GrandpaB,

Just use a datatable, that has all that you ask standard in system.data

I hope this helps,

Cor
Nov 21 '05 #2

P: n/a
GrandpaB,

Or if you for what ever reason don't want a datatable (I never saw here
anything else than silly reason for that, with the except from somebody who
told he did it to study), than make yourself a new arrayclass using
collectionbase

http://msdn.microsoft.com/library/de...classtopic.asp

I hope this helps,

Cor

"GrandpaB" <Gr******@discussons.microsoft.com> schreef in bericht
news:AD**********************************@microsof t.com...
I need to sort an Arraylist. The Arraylist is contained in classArt.
The
Arraylist contains objects that have been defined in a sperate class,
objArt.
I have attempted to implement the sort using IComparable, but I'm
hopelessly
lost. With the addition of "Implements ICompariable" and the "CompareTo"
function the program will ont even compile.
Any suggestion that you have would be gratefully welcomed. I am
including the relevant code:

Public Class classArt
Implements IComparable

Private Shared ArtList As New ArrayList
Private ArtIndex As Integer
Private ArtFileName As String = "art.xml"

Public Sub SaveFile() 'save ArtList to ArtFile
Dim Msg1 As String = "ArtFile was not saved! Reason: "
Dim Msg2 As String = "Art"
Dim sf As New SoapFormatter
Dim fs As New FileStream(ArtFileName, FileMode.Create)
Try
sf.Serialize(fs, ArtList)
Catch ex As Exception
MsgBox(Msg1 & ex.Message, MsgBoxStyle.Critical, Msg2)
Finally
fs.Close()
End Try
End Sub

Public Sub GetFile() 'get ArtList from ArtFile
Dim Msg1 As String = "Failed to open ArtList! Reason: "
Dim Msg2 As String = "Art"
If System.IO.File.Exists(ArtFileName) Then
Dim fs As New FileStream(ArtFileName, FileMode.Open)
Dim sf As New SoapFormatter
Try
ArtList = CType(sf.Deserialize(fs), ArrayList)
Catch ex As Exception
MsgBox(Msg1 & ex.Message, MsgBoxStyle.Critical, Msg2)
Finally
fs.Close()
sf = Nothing
fs = Nothing
End Try
Else
AddArt()
End If
End Sub

Public Function CompareTo(ByVal xArt As objArt) As Integer Implements
System.IComparable.CompareTo
Dim xDes As String
xDes = xArt.Des
Return "hi".CompareTo(xDes)
End Function

Public Sub AddArt()
'add an empty objArt to ArtList
Dim emptyArt As New objArt
With emptyArt
.Des = ""
.Title = ""
.Pic = ""
.Type = ""
.Room = ""
.Location = ""
.Height = 0
.Width = 0
.Depth = 0
.Bequeth = ""
.Appraised = ""
.Value = 0
End With
ArtList.Add(emptyArt)
emptyArt = Nothing
End Sub

End Class

<[Serializable]()> Public Class objArt
'Implements IComparable

Public Title As String
Public Des As String
Public Pic As String
Public Type As String
Public Room As String
Public Location As String
Public Height As Integer
Public Width As Integer
Public Depth As Integer
Public Bequeth As String
Public Appraised As String
Public Value As Integer

End Class
--
GrandpaB

Nov 21 '05 #3

P: n/a
Hi,

There is a sortedlist class
http://msdn.microsoft.com/library/de...classtopic.asp

Ken
-----------------------
"GrandpaB" <Gr******@discussons.microsoft.com> wrote in message
news:AD**********************************@microsof t.com...
I need to sort an Arraylist. The Arraylist is contained in classArt.
The
Arraylist contains objects that have been defined in a sperate class,
objArt.
I have attempted to implement the sort using IComparable, but I'm
hopelessly
lost. With the addition of "Implements ICompariable" and the "CompareTo"
function the program will ont even compile.
Any suggestion that you have would be gratefully welcomed. I am
including the relevant code:

Public Class classArt
Implements IComparable

Private Shared ArtList As New ArrayList
Private ArtIndex As Integer
Private ArtFileName As String = "art.xml"

Public Sub SaveFile() 'save ArtList to ArtFile
Dim Msg1 As String = "ArtFile was not saved! Reason: "
Dim Msg2 As String = "Art"
Dim sf As New SoapFormatter
Dim fs As New FileStream(ArtFileName, FileMode.Create)
Try
sf.Serialize(fs, ArtList)
Catch ex As Exception
MsgBox(Msg1 & ex.Message, MsgBoxStyle.Critical, Msg2)
Finally
fs.Close()
End Try
End Sub

Public Sub GetFile() 'get ArtList from ArtFile
Dim Msg1 As String = "Failed to open ArtList! Reason: "
Dim Msg2 As String = "Art"
If System.IO.File.Exists(ArtFileName) Then
Dim fs As New FileStream(ArtFileName, FileMode.Open)
Dim sf As New SoapFormatter
Try
ArtList = CType(sf.Deserialize(fs), ArrayList)
Catch ex As Exception
MsgBox(Msg1 & ex.Message, MsgBoxStyle.Critical, Msg2)
Finally
fs.Close()
sf = Nothing
fs = Nothing
End Try
Else
AddArt()
End If
End Sub

Public Function CompareTo(ByVal xArt As objArt) As Integer Implements
System.IComparable.CompareTo
Dim xDes As String
xDes = xArt.Des
Return "hi".CompareTo(xDes)
End Function

Public Sub AddArt()
'add an empty objArt to ArtList
Dim emptyArt As New objArt
With emptyArt
.Des = ""
.Title = ""
.Pic = ""
.Type = ""
.Room = ""
.Location = ""
.Height = 0
.Width = 0
.Depth = 0
.Bequeth = ""
.Appraised = ""
.Value = 0
End With
ArtList.Add(emptyArt)
emptyArt = Nothing
End Sub

End Class

<[Serializable]()> Public Class objArt
'Implements IComparable

Public Title As String
Public Des As String
Public Pic As String
Public Type As String
Public Room As String
Public Location As String
Public Height As Integer
Public Width As Integer
Public Depth As Integer
Public Bequeth As String
Public Appraised As String
Public Value As Integer

End Class
--
GrandpaB

Nov 21 '05 #4

P: n/a
Thank you for your suggestions. As you can tell, I'm new to VB.Net & am not
familiar with all the resourceses of the language. I chose to create the art
data in an XML file because it would contain only 250 items at most and so
that the data file would be viewable in a browser. The Arraylist looked
inviting with all its methods including the sort method. Are you, as MVP's,
suggesting that my uninformed choice was not workable? Is there no way to
make the ArrayList work with IComparable or would it be so difficult that I
should just start over?

In reviewing your suggestions, I think that I'll attempt to add a SortedList
subclass to the class, classArt. The key in the Sortedlist key will contain
the values from the Arraylist that I want to sort on and Sortedlist value
will contain the index of the Arraylist. Do you have any other words of
wisdom before I embark on this approach?

Thanks again.
--
GrandpaB

Nov 21 '05 #5

P: n/a
Grandpa,

That XML does let me choose now even more for a dataset/datatable

\\\
dim ds as new dataset
dim dt as new datatable
ds.tables.add(dt)
for each i as integer = 1 to 250
dt.columns.add("i.toString")
next 'this makes 250 columns with the name 1 to 250
for i = 0 to 1
dt.addrow(dt.newrow)
for y = 0 to 249
dt(i)(y) = i.tostring
next
next ' this makes 2 rows filled all with 0 and 2
ds.writexml("c:\grandpa.xml")
ds.tables(0).defaultview.sort = "1" 'sort on the column with the name 1
datagrid1.datasource = ds.tables(0).defaultview
ds.readxml("c:\grandpa.xml")
///

In my opinion do you have here roughly typed all that you need.

I hope this helps,

Cor

"GrandpaB" <Gr******@discussons.microsoft.com> schreef in bericht
news:49**********************************@microsof t.com...
Thank you for your suggestions. As you can tell, I'm new to VB.Net & am
not
familiar with all the resourceses of the language. I chose to create the
art
data in an XML file because it would contain only 250 items at most and so
that the data file would be viewable in a browser. The Arraylist looked
inviting with all its methods including the sort method. Are you, as
MVP's,
suggesting that my uninformed choice was not workable? Is there no way to
make the ArrayList work with IComparable or would it be so difficult that
I
should just start over?

In reviewing your suggestions, I think that I'll attempt to add a
SortedList
subclass to the class, classArt. The key in the Sortedlist key will
contain
the values from the Arraylist that I want to sort on and Sortedlist value
will contain the index of the Arraylist. Do you have any other words of
wisdom before I embark on this approach?

Thanks again.
--
GrandpaB

Nov 21 '05 #6

P: n/a
Cor,

Thanks; I had not realized that it was so easy to create a datatable from
and XML file. You have outlined an intreguing approach and if I can't get
the IComparable to work I'll give your suggestion a try.
--
GrandpaB

Nov 21 '05 #7

P: n/a
After finding an example about Collections, I believe that the IComparable
interface and the CompareTo function should be in the class, objArt. Then
the Arraylist, which is list of objArt objects, can be sorted by calling the
Arraylist sort method, Arraylist.Sort().

This is the current state of the objArt class; it contains two errors! Any
help eliminating these errors would be appreciated?

<[Serializable]()> Public Class objArt
Implements IComparable 'Error #1

Public Title As String
Public Des As String
Public Pic As String
Public Type As String
Public Room As String
Public Location As String
Public Height As Integer
Public Width As Integer
Public Depth As Integer
Public Bequeth As String
Public Appraised As String
Public Value As Integer

Public Function CompareTo(ByVal xArt As objArt) As Integer _
Implements System.IComparable.CompareTo 'Error #2
Return Me.Title.CompareTo(xArt.Title)
End Function

End Class

Error #1 - 'Art2.objArt' must implement 'Overridable Function CompareTo(obj
As Object) As Integer' for interface 'System.IComparable'.

Error#2 - 'CompareTo' cannot implement 'CompareTo' because there is no
matching function on interface 'IComparable'.

--
GrandpaB

Nov 21 '05 #8

P: n/a
Problem Solved!!!

The function CompareTo is fussy; it wants the parameter xArt to be passed as
an object, not objArt.

Public Function CompareTo(ByVal xArt As Object) As Integer _
Implements System.IComparable.CompareTo 'Error #2
Return Me.Title.CompareTo(xArt.Title)
End Function

--
GrandpaB
"GrandpaB" wrote:
After finding an example about Collections, I believe that the IComparable
interface and the CompareTo function should be in the class, objArt. Then
the Arraylist, which is list of objArt objects, can be sorted by calling the
Arraylist sort method, Arraylist.Sort().

This is the current state of the objArt class; it contains two errors! Any
help eliminating these errors would be appreciated?

<[Serializable]()> Public Class objArt
Implements IComparable 'Error #1

Public Title As String
Public Des As String
Public Pic As String
Public Type As String
Public Room As String
Public Location As String
Public Height As Integer
Public Width As Integer
Public Depth As Integer
Public Bequeth As String
Public Appraised As String
Public Value As Integer

Public Function CompareTo(ByVal xArt As objArt) As Integer _
Implements System.IComparable.CompareTo 'Error #2
Return Me.Title.CompareTo(xArt.Title)
End Function

End Class

Error #1 - 'Art2.objArt' must implement 'Overridable Function CompareTo(obj
As Object) As Integer' for interface 'System.IComparable'.

Error#2 - 'CompareTo' cannot implement 'CompareTo' because there is no
matching function on interface 'IComparable'.

--
GrandpaB

Nov 21 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.