473,398 Members | 2,393 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,398 software developers and data experts.

How to sort an array of objects

Hi,

I've created my own class containing a few properties like name, zip-code
and savings.
At some point i'm storing several objects in an array.

Does anyone know how to sort the array using the zip-code, again using the
savings and last sorting by name?
(Tripple-sort)

Thanx

John
Mar 8 '07 #1
2 3396
The details (particularly of repeated sorts) is hard to cover in a newsgroup
thread but I will suggest that you need to implement the IComparable
Interface in your custom class. This is the way that the Array.Sort method
knows how to sort things which it has no idea about... it asks your objects
which do know.

If you always want to always sort on the same three properties you can
simply concatenate the values. If on the other hand you want to sort on any
property (or any combination of properties) at any time you have more work.

Hope this helps,
Tom

"John Devlon" <jo********@hotmail.comwrote in message
news:ET*******************@phobos.telenet-ops.be...
Hi,

I've created my own class containing a few properties like name, zip-code
and savings.
At some point i'm storing several objects in an array.

Does anyone know how to sort the array using the zip-code, again using the
savings and last sorting by name?
(Tripple-sort)

Thanx

John

Mar 8 '07 #2
John Devlon wrote:
I've created my own class containing a few properties like name, zip-code
and savings.
At some point i'm storing several objects in an array.

Does anyone know how to sort the array using the zip-code, again using the
savings and last sorting by name?
(Tripple-sort)
As Tom pointed out, if you want custom sorting one of your bets is to
implement the IComparable interface in your class. Something in the
likes of:

<aircode>
'inside your class, say, Person
Implements IComparable

'...

Public Function CompareTo( _
ByVal Obj As Object _
) As Integer _
Implements System.IComparable.CompareTo

'Assumes a ZipCode/Savings/Naming order

Dim Other As Person = TryCast(Obj, Person)
If Other Is Nothing Then Return 1
Dim Result As Integer

Result = Me.ZipCode.CompareTo(Other.ZipCode)
If Result = 0 Then
Result = Me.Savings.CompareTo(Other.Savings)
If Result = 0 Then
Result = Me.Name.CompareTo(Other.Name)
End If
End If
Return Result

End Function
</aircode>
Now, if you need more flexibility, you may consider creating a class
that implements IComparer. The advantage is that most collections
allow you to provide an IComparer that will take precedence over the
IComparable interface of the class being sorted. This gives you the
opportunity to create a completely configurable sorting, just by
passing the appropriate comparer to the sorting routine.

(In the code bellow, I'm assuming you're using VB 2005, which allows
generics, but the principles would be the same for a non-generic
IComparer -- but you'd have to raise exceptions in case of parameters
of types incompatible with your class' type)

<aircode>
Public Class PersonComparer
Implements IComparer(Of Person)

'Assumes a Name/Savings/ZipCode order
Public Function Compare( _
ByVal X As Person, ByVal Y As Person) As Integer _
Implements System.Collections. _
Generic.IComparer(Of Person).Compare

Dim Result As Integer
If X Is Nothing Then
If Y IsNot Nothing Then Result = -1
ElseIf Y Is Nothing Then
Result = 1
Else

Result = String.Compare(X.Name, Y.Name, ignoreCase:=True)

If Result = 0 Then
Result = X.Savings.CompareTo(Y.Savings)
If Result = 0 Then
Result = X.ZipCode.CompareTo(Y.ZipCode)
End If
End If

End If
Return Result
End Function

End Class
</aircode>

HTH.

Regards,

Branco.

Mar 8 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: its me | last post by:
Let's say I have a class of people... Public Class People Public Sex as String Public Age as int Public Name as string end class And I declare an array of this class...
40
by: Elijah Bailey | last post by:
I want to sort a set of records using STL's sort() function, but dont see an easy way to do it. I have a char *data; which has size mn bytes where m is size of the record and n is the...
8
by: laniik | last post by:
Hi. I have a problem using STL's built in sort that seems impossible to get around. if i have: -------------------------------- struct object { int val; }
4
by: PCHOME | last post by:
Hi! I have questions about qsort( ). Is anyone be willing to help? I use the following struct: struct Struct_A{ double value; ... } *AA, **pAA;
7
by: Daniel | last post by:
does C# have any collection objects that support sort functionality so that I dont have to write my own sorting algorithm?
21
by: yeti349 | last post by:
Hi, I'm using the following code to retrieve data from an xml file and populate a javascript array. The data is then displayed in html table form. I would like to then be able to sort by each...
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...
3
by: fdu.xiaojf | last post by:
Hi, It seems that an array acts like an list very much, except it doesn't have a method sort. Regards,
0
by: JosAH | last post by:
Greetings, I was asked to write a Tip Of the Week; so here goes: a lot of topics are started here in this forum (and a lot of other forums too) mentioning a problem about sorting data. ...
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: 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
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.