Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > sort problem sorting a generic list

Reply
Thread Tools

sort problem sorting a generic list

 
 
Paul
Guest
Posts: n/a
 
      09-04-2008
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
--
Paul G
Software engineer.
 
Reply With Quote
 
 
 
 
sloan
Guest
Posts: n/a
 
      09-04-2008
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.




"Paul" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
> --
> Paul G
> Software engineer.



 
Reply With Quote
 
 
 
 
sloan
Guest
Posts: n/a
 
      09-04-2008


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.......




"Paul" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
> --
> Paul G
> Software engineer.



 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      09-04-2008
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.GroupNumbe r);
if (returnValue == 0)
{
returnValue = this.Count.CompareTo(other.Count);//tie
}
return returnValue;
//orig return GroupNumber.CompareTo(other.GroupNumber);

}
--
Paul G
Software engineer.


"sloan" wrote:

>
>
> 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.......
>
>
>
>
> "Paul" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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
> > --
> > Paul G
> > Software engineer.

>
>
>

 
Reply With Quote
 
sloan
Guest
Posts: n/a
 
      09-05-2008

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" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.GroupNumbe r);
> if (returnValue == 0)
> {
> returnValue = this.Count.CompareTo(other.Count);//tie
> }
> return returnValue;
> //orig return GroupNumber.CompareTo(other.GroupNumber);
>
> }
> --
> Paul G
> Software engineer.
>
>
> "sloan" wrote:
>
>>
>>
>> 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.......
>>
>>
>>
>>
>> "Paul" <(E-Mail Removed)> wrote in message
>> news:(E-Mail Removed)...
>> > 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
>> > --
>> > Paul G
>> > Software engineer.

>>
>>
>>



 
Reply With Quote
 
sloan
Guest
Posts: n/a
 
      09-05-2008

http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!141.entry

That is a 1.1 article, however, there are a few 2.0 hints at the bottom.
having said that, I would keep away from the reflection based
comparer's/sorter's .... if I had any signficant sized lists.


..........


"sloan" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> 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" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
>> 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.GroupNumbe r);
>> if (returnValue == 0)
>> {
>> returnValue = this.Count.CompareTo(other.Count);//tie
>> }
>> return returnValue;
>> //orig return GroupNumber.CompareTo(other.GroupNumber);
>>
>> }
>> --
>> Paul G
>> Software engineer.
>>
>>
>> "sloan" wrote:
>>
>>>
>>>
>>> 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.......
>>>
>>>
>>>
>>>
>>> "Paul" <(E-Mail Removed)> wrote in message
>>> news:(E-Mail Removed)...
>>> > 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
>>> > --
>>> > Paul G
>>> > Software engineer.
>>>
>>>
>>>

>
>



 
Reply With Quote
 
Paul
Guest
Posts: n/a
 
      09-08-2008
Ok thanks it works, just using an artificial row number. Guessing that
reflection based comparer's/sorter's can have an impact on performance.
--
Paul G
Software engineer.


"sloan" wrote:

>
> http://sholliday.spaces.live.com/Blog/cns!A68482B9628A842A!141.entry
>
> That is a 1.1 article, however, there are a few 2.0 hints at the bottom.
> having said that, I would keep away from the reflection based
> comparer's/sorter's .... if I had any signficant sized lists.
>
>
> ..........
>
>
> "sloan" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> >
> > 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" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed)...
> >> 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.GroupNumbe r);
> >> if (returnValue == 0)
> >> {
> >> returnValue = this.Count.CompareTo(other.Count);//tie
> >> }
> >> return returnValue;
> >> //orig return GroupNumber.CompareTo(other.GroupNumber);
> >>
> >> }
> >> --
> >> Paul G
> >> Software engineer.
> >>
> >>
> >> "sloan" wrote:
> >>
> >>>
> >>>
> >>> 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.......
> >>>
> >>>
> >>>
> >>>
> >>> "Paul" <(E-Mail Removed)> wrote in message
> >>> news:(E-Mail Removed)...
> >>> > 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
> >>> > --
> >>> > Paul G
> >>> > Software engineer.
> >>>
> >>>
> >>>

> >
> >

>
>
>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Sorting list vs sorting vector boltar2003@boltar.world C++ 2 07-06-2010 09:40 AM
generic interfaces with generic methods Murat Tasan Java 1 02-03-2009 12:17 PM
Using s.sort([cmp[, key[, reverse]]]) to sort a list of objects based on a attribute cjt22@bath.ac.uk Python 7 09-10-2007 11:10 AM
Sorting a filtering a generic list bound to a paged GridView? Harry Haller ASP .Net 3 02-01-2007 10:08 PM
Ado sort error-Ado Sort -Relate, Compute By, or Sort operations cannot be done on column(s) whose key length is unknown or exceeds 10 KB. Navin ASP General 1 09-09-2003 07:16 AM



Advertisments