sort problem sorting a generic list

Discussion in 'ASP .Net' started by Paul, Sep 4, 2008.

  1. Paul

    Paul Guest

    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.
    Paul, Sep 4, 2008
    #1
    1. Advertising

  2. Paul

    sloan Guest

    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" <> wrote in message
    news:...
    > 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.
    sloan, Sep 4, 2008
    #2
    1. Advertising

  3. Paul

    sloan Guest

    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" <> wrote in message
    news:...
    > 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.
    sloan, Sep 4, 2008
    #3
  4. Paul

    Paul Guest

    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" 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" <> wrote in message
    > news:...
    > > 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.

    >
    >
    >
    Paul, Sep 4, 2008
    #4
  5. Paul

    sloan Guest

    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" <> wrote in message
    news:...
    > 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" 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" <> wrote in message
    >> news:...
    >> > 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.

    >>
    >>
    >>
    sloan, Sep 5, 2008
    #5
  6. Paul

    sloan Guest

    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" <> wrote in message
    news:...
    >
    > 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" <> wrote in message
    > news:...
    >> 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" 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" <> wrote in message
    >>> news:...
    >>> > 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.
    >>>
    >>>
    >>>

    >
    >
    sloan, Sep 5, 2008
    #6
  7. Paul

    Paul Guest

    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" <> wrote in message
    > news:...
    > >
    > > 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" <> wrote in message
    > > news:...
    > >> 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" 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" <> wrote in message
    > >>> news:...
    > >>> > 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.
    > >>>
    > >>>
    > >>>

    > >
    > >

    >
    >
    >
    Paul, Sep 8, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Murat Tasan
    Replies:
    1
    Views:
    8,027
    Chaitanya
    Feb 3, 2009
  2. Harry Haller
    Replies:
    3
    Views:
    1,181
    RobinS
    Feb 1, 2007
  3. Replies:
    7
    Views:
    731
    Stefan Arentz
    Sep 10, 2007
  4. Replies:
    2
    Views:
    1,407
    James Kanze
    Jul 6, 2010
  5. Navin
    Replies:
    1
    Views:
    671
    Ken Schaefer
    Sep 9, 2003
Loading...

Share This Page