473,387 Members | 1,798 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,387 software developers and data experts.

Using ICompare to sort on two properties

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.

/// </summary>

/// <param name="x">UserName</param>

/// <param name="y">UserName</param>

/// <returns></returns>

public string Compare(object x, object y)

{

UserName UserName X = (UserName )x;

UserName MapItemY = (UserName )y;

switch (m_Column)

{

case UserName .UserName SortColumns.FNameLName:

{

switch (m_SortOrder)

{

case Common.SortOrderTypes.Ascending:

//Not sure how to setup the compare to compare two properties

return UserNameX.?.CompareTo(UserNameY.?);

case Common.SortOrderTypes.Descending:

return UserNameY.?.CompareTo(UserNameX.?);
}

break;

}

case MapItem.MapItemSortColumns.LNameFName:

{

switch (m_SortOrder)

{

case Common.SortOrderTypes.Ascending:

//Not sure how to setup the compare to compare two properties

return UserNameX.?.CompareTo(UserNameY.?);

case Common.SortOrderTypes.Descending:

return UserNameY.?.CompareTo(UserNameX.?);

}

break;

}

}

return UserNameX.ItemKey.CompareTo(UserNameY.ItemKey);

}

}
Nov 16 '05 #1
1 9766

"MFRASER" wrote...
How do I setup my ICompare to sort on two properties?


That depends on which property is the most "significant".

If there's a "difference" when you compare the most significant property,
return that result.

If they're "equal", compare the other property and return that result.

BTW, the Compare method should return an int, not a string...

Example:
public int Compare(object x, object y)
{
Person userX = (Person) x;
Person userY = (Person) y;

if ( userX.LastName.Equals(userY.LastName) )
{
return userX.FirstName.CompareTo(userY.FirstName);
}

return userX.LastName.CompareTo(userY.LastName);
}
// Bjorn A
Nov 16 '05 #2

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

Similar topics

5
by: Kenneth McDonald | last post by:
Now that I'm back to Python and all the new (to me) cool features, I find I'm using properties a lot, i.e. I'm defining: foo = property(fset=..., fget=...) for a number of properties, in many...
9
by: MacDermott | last post by:
I have an Access MDB which instantiates a class in a custom DLL, manipulates it for a while, then sets it equal nothing. The MDB does other things,too, and generally behaves itself as desired....
1
by: Prasad Karunakaran | last post by:
I am using the C# DirectoryEntry class to retrieve the Properties of an user object in the Active Directory. I need to get the First Name and Last Name as properties. I know it is not supported...
0
by: dhnriverside | last post by:
Hi peeps, I had a function working fine for retrieving a list of users from Active Directory. However, client wants it sorted alphabetically. So I added Sort option to my searcher, and now the...
0
by: Brian Young | last post by:
Hi all. I'm using the Property Grid control in a control to manage a windows service we have developed here. The windows service runs a set of other jobs that need to be managed. The control...
1
by: Mike Rayl | last post by:
Does anyone have example code for putting a direction arrow indicating the sort order of a column in a listview. I have the sorting code already for each column using icompare. I've found some code...
16
by: Dennis | last post by:
I have a class named "myclass" and an arraylist containing elements of type "MyClass". I want to get the value of a property of "MyClass" (a string type) for one of the arraylist elements. I...
3
by: Stu | last post by:
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...
1
by: jmdolinger | last post by:
Hi all, I'm a newbie to Atlas (and recently ASP.NET) after coming from a long Java background, also have done quite a bit with an Ajax.NET/ASP.NET 1.1 project, but it was basically all...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.