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

multiple property sort using icompare

Stu
Hi,

i have seen lots of code showing how you can use the icomparer interface to
provide sorts on arrays of structures.However i want to compare two fields.
Say I have a portfolio code and asset code i would like to sort my arraylist
of class objects by asset codes within portfolios. then sorted in portfolio
order

TIA

Stu
Nov 21 '05 #1
3 2383
Your IComparer only compares two of your object instances. In the code
for the Compare, you just need to specify whether one of them is
'greater' or 'less' than the other. When you implement the Compare
function, first compare the portfolio code and only compare the asset
code if the portfolio code is the same (watch for typos):

Public Function Compare(x As Object, y As Object) As Integer
Dim obj1, obj2 As TheClass

'Code here to verify that x and y are both instances of TheClass

obj1 = DirectCast(x, TheClass)
obj2 = DirectCast(y, TheClass)

If obj1.Portfolio < obj2.Portfolio Then
Return -1
Else
If obj2.Portfolio > obj1.Portfolio Then
Return 1
Else
If obj1.Asset < obj2.Asset Then
Return -1
Else
If obj2.Asset > obj1.Asset Then
Return 1
Else
Return 0
End If
End If
End If
End If

End Function

This is code off the top of my head so watch for typos.

Chris

Nov 21 '05 #2
Stu,
As Chris suggests you need to compare both properties of both objects in the
IComparer.Compare method.

I normally use something like:

Public Class Stock
Public Portfolio As String

Public Asset As String
End Class

Public Class StockComparer
Implements IComparer

Public Function Compare(ByVal x As Object, ByVal y As Object) As
Integer Implements IComparer.Compare
Dim xStock As Stock = DirectCast(x, Stock)
Dim yStock As Stock = DirectCast(y, Stock)

If xStock.Portfolio < yStock.Portfolio Then
Return -1
ElseIf xStock.Portfolio > yStock.Portfolio Then
Return 1
ElseIf xStock.Asset < yStock.Asset Then
Return -1
ElseIf xStock.Asset > yStock.Asset Then
Return 1
Else
Return 0
End If
End Function

End Class

Which is a variation of what Chris has.

Hope this helps
Jay

"Stu" <St*@discussions.microsoft.com> wrote in message
news:84**********************************@microsof t.com...
| Hi,
|
| i have seen lots of code showing how you can use the icomparer interface
to
| provide sorts on arrays of structures.However i want to compare two
fields.
| Say I have a portfolio code and asset code i would like to sort my
arraylist
| of class objects by asset codes within portfolios. then sorted in
portfolio
| order
|
| TIA
|
| Stu
|
|
Nov 21 '05 #3
Stu
Ah cool and thanks guys
I new it was something simple!!!
"Jay B. Harlow [MVP - Outlook]" wrote:
Stu,
As Chris suggests you need to compare both properties of both objects in the
IComparer.Compare method.

I normally use something like:

Public Class Stock
Public Portfolio As String

Public Asset As String
End Class

Public Class StockComparer
Implements IComparer

Public Function Compare(ByVal x As Object, ByVal y As Object) As
Integer Implements IComparer.Compare
Dim xStock As Stock = DirectCast(x, Stock)
Dim yStock As Stock = DirectCast(y, Stock)

If xStock.Portfolio < yStock.Portfolio Then
Return -1
ElseIf xStock.Portfolio > yStock.Portfolio Then
Return 1
ElseIf xStock.Asset < yStock.Asset Then
Return -1
ElseIf xStock.Asset > yStock.Asset Then
Return 1
Else
Return 0
End If
End Function

End Class

Which is a variation of what Chris has.

Hope this helps
Jay

"Stu" <St*@discussions.microsoft.com> wrote in message
news:84**********************************@microsof t.com...
| Hi,
|
| i have seen lots of code showing how you can use the icomparer interface
to
| provide sorts on arrays of structures.However i want to compare two
fields.
| Say I have a portfolio code and asset code i would like to sort my
arraylist
| of class objects by asset codes within portfolios. then sorted in
portfolio
| order
|
| TIA
|
| Stu
|
|

Nov 21 '05 #4

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

Similar topics

2
by: Clinton Pierce | last post by:
I've filled a DataTable with columns that have custom type (a class that I'm using to keep track of other things, not just a value). When the .Select method goes to sort this column, how do I let...
1
by: MFRASER | last post by:
How do I setup my ICompare to sort on two properties? /// <summary> /// Compares two objects and returns a value indicating whether one is less than, equal to or greater than the other. ///...
2
by: areef.islam | last post by:
Hi, I am kinda new to javascript and I am having this problem with selecting multiple options from a select tag. Hope someone can help me out here. here is my code...
7
by: =?Utf-8?B?TG9zdEluTUQ=?= | last post by:
Hi All :) I'm converting VB6 using True DBGrid Pro 8.0 to VB2005 using DataGridView. True DBGrid has a MultipleLines property that controls whether individual records span multiple lines. Is...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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.