IComparer Sorting based on 2 object vales

A

Arulraja

Hello,

I have a custom collection object. each instance has FirstName and
LastName. I want to sort this object collection by Last Name and First
Name(ie if the last names are same I want to sub sort it by First
Name).

I know how to sort it by one value?(last Name) I am using the
following code for this.It is working fine.

public int CompareTo(Person obj, SortOrder sort, bool sortAscending)
{
int retVal = 0;
switch (sort)
{
case SortOrder.LastName:
{
retVal = this.LastName.CompareTo(obj.LastName);
if (sortAscending)
return retVal;
else
return (-1 * retVal);
}
//This Part is not working
------------------------------------------------------------------
case SortOrder.LastNameAndFirstName:
{
retVal = this.LastName.CompareTo(obj.LastName);
retVal = this.FirstName.CompareTo(obj.FirstName);
if (sortAscending)
return retVal;
else
return (-1 * retVal);
}
------------------------------------------------------------------
}
return retVal;
}


Can somebody direct me on this.

Thanks
Arul
 
M

mikeb

Arulraja said:
Hello,

I have a custom collection object. each instance has FirstName and
LastName. I want to sort this object collection by Last Name and First
Name(ie if the last names are same I want to sub sort it by First
Name).

I know how to sort it by one value?(last Name) I am using the
following code for this.It is working fine.

public int CompareTo(Person obj, SortOrder sort, bool sortAscending)
{
int retVal = 0;
switch (sort)
{
case SortOrder.LastName:
{
retVal = this.LastName.CompareTo(obj.LastName);
if (sortAscending)
return retVal;
else
return (-1 * retVal);
}
//This Part is not working
------------------------------------------------------------------
case SortOrder.LastNameAndFirstName:
{
retVal = this.LastName.CompareTo(obj.LastName);
retVal = this.FirstName.CompareTo(obj.FirstName);
if (sortAscending)
return retVal;
else
return (-1 * retVal);
}

You're throwing away the information from the LastName comparison. I
think you need to do something like:

case SortOrder.LastNameAndFirstName:
{
retVal = this.LastName.CompareTo(obj.LastName);

// only compare first name when last name is same
if (retVal == 0) {
retVal = this.FirstName.CompareTo(obj.FirstName);
}

if (!sortAscending) {
retVal = (-1 * retVal);
}

return( retVal);
}
 
A

Arulraja

Thanks a lot Mike.

mikeb said:
You're throwing away the information from the LastName comparison. I
think you need to do something like:

case SortOrder.LastNameAndFirstName:
{
retVal = this.LastName.CompareTo(obj.LastName);

// only compare first name when last name is same
if (retVal == 0) {
retVal = this.FirstName.CompareTo(obj.FirstName);
}

if (!sortAscending) {
retVal = (-1 * retVal);
}

return( retVal);
}
 

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