sort problem sorting a generic list

P

Paul

Hi, I have a generic list and am able to sort it using the code below that I
have built in a class UserDefinedGroupSummary. There is an integer in the
class called GroupNumber and the sort by group number does return a list with
the group numbers sorted. The problem is when there is only one group number
in the list, it looks like it is still changing the order of the elements in
the list.
Any ideas, thanks.

I use grouplist.Sort();// to sort the group generic list

In the class I have
public static Comparison<UserDefinedGroupSummary> CompareGroup =
delegate(UserDefinedGroupSummary g1, UserDefinedGroupSummary g2)
{
return g1.GroupNumber.CompareTo(g2.GroupNumber);
};
#region IComparable<UserDefinedGroupSummary> Members

public int CompareTo(UserDefinedGroupSummary other)
{
return GroupNumber.CompareTo(other.GroupNumber);
}
#endregion
 
S

sloan

If you have a "tie" (result of 0) you need to break it, if you dont' want
random results.


public int CompareTo(UserDefinedGroupSummary other)
{
int returnValue = 0;

returnValue =GroupNumber.CompareTo(other.GroupNumber);

if (returnValue ==0)
{ returnValue = this.CreateDate.CompareTo(other.CreateDate); //
or some property
}

return returnValue;
}

pseudo code for you to experiment with.
 
S

sloan

Here is a sample of my LastName/FirstName sorter for a User object.


returnValue = this.LastName.CompareTo(other.LastName);

if (returnValue == 0) //tie

{

returnValue = this.FirstName.CompareTo(other.FirstName);

}

return returnValue ;


Same concept.......
 
P

Paul

Hi thanks for the response. I made the modification as suggested. I do not
have a row number but thinking I may need to add one and then if tie
condition have it sort on the row number. What I have below .Count is always
0 for all records so it still is mixing things up.
Thanks again

public int CompareTo(UserDefinedGroupSummary other)
{
int returnValue = 0;//added
returnValue=GroupNumber.CompareTo(other.GroupNumber);
if (returnValue == 0)
{
returnValue = this.Count.CompareTo(other.Count);//tie
}
return returnValue;
//orig return GroupNumber.CompareTo(other.GroupNumber);

}
--
Paul G
Software engineer.


sloan said:
Here is a sample of my LastName/FirstName sorter for a User object.


returnValue = this.LastName.CompareTo(other.LastName);

if (returnValue == 0) //tie

{

returnValue = this.FirstName.CompareTo(other.FirstName);

}

return returnValue ;


Same concept.......
 
S

sloan

You understand the concept, so now you have to find your solution.

If a two way tie isn't enough, then go to something else.

In my example...
LastName
FirstName

If I find out I have a bunch of "John Smith"'s in my collection, then I
could go to the SSN or something.

............

If you're using an IDataReader or something like that to populate, then yes,
you can can add an artificial "RowNumber" property if you want.
Its your call. The bottom line is that if you have a tie, you can't predict
the results unless you break the tie.


Good luck.




Paul said:
Hi thanks for the response. I made the modification as suggested. I do
not
have a row number but thinking I may need to add one and then if tie
condition have it sort on the row number. What I have below .Count is
always
0 for all records so it still is mixing things up.
Thanks again

public int CompareTo(UserDefinedGroupSummary other)
{
int returnValue = 0;//added
returnValue=GroupNumber.CompareTo(other.GroupNumber);
if (returnValue == 0)
{
returnValue = this.Count.CompareTo(other.Count);//tie
}
return returnValue;
//orig return GroupNumber.CompareTo(other.GroupNumber);

}
 
P

Paul

Ok thanks it works, just using an artificial row number. Guessing that
reflection based comparer's/sorter's can have an impact on performance.
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top