Sort an Arraylist

G

Guest

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
 
C

Cor Ligthert [MVP]

GrandpaB,

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

I hope this helps,

Cor
 
G

Guest

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.
 
C

Cor Ligthert [MVP]

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
 
G

Guest

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.
 
G

Guest

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'.
 
G

Guest

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top