473,395 Members | 1,666 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Sort an Arraylist

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
8 2620
GrandpaB,

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

I hope this helps,

Cor
Nov 21 '05 #2
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Leon | last post by:
I have six textbox controls on my webform that allows the user to enter any numbers from 1 to 25 in any order. However, I would like to sort those numbers from least to greatest before sending them...
1
by: Gunjan Garg | last post by:
Hello All, I am working to create a generic datagrid which accepts a datasource(ListData - This is our own datatype) and depending on the calling program customizes itself for sorting,...
3
by: Adam J. Schaff | last post by:
Hello. I recently noticed that the Sort method of the .NET ArrayList class does not behave as I expected. I expect 'A' < '_' < 'a' (as per their ascii values) but what I got was the opposite....
19
by: Derek Martin | last post by:
Hi there, I have been playing with sorting my arraylist and having some troubles. Maybe just going about it wrong. My arraylist contains objects and one of the members of the object is 'name.' I...
0
by: Grant Wickman | last post by:
Our team has just fixed a really nasty problem that appears to be caused by an obscure bug in the sort method of Arraylist and daisy-chained webservice stubs. We've fixed the bug so I'll not...
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 same data. Same data on the same CPU gets sorted a...
4
by: M Harvey | last post by:
I have an arraylist that contains datetime values. What is the best way to sort this arraylist by date ascending? Thanks, Matt
3
by: jtfaulk | last post by:
Re: ArrayList, Sort, Menu, IComparer, Object, multidemensional I have a multi-dimensional arraylist, and I would like to sort one level of it but not all. The multi-dimensional arraylist...
4
by: rn5a | last post by:
Can the items in a ListBox be sorted by the name of the items? The ListBox actually lists all directories & files existing in a directory on the server. Note that all the directories should be...
3
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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
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...
0
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...

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.