Sorting a list Structure

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike Copeland

    Sorting a list Structure

    I'm confused about how to sort a list on an element of the list's
    structure. Here is my data definition:

    struct Finishes // computed Finish information
    {
    long netTime;
    int bibNumber;
    } fWork;
    typedef list<FinishesFI NISHES;
    FINISHES finishInfo;
    list<Finishes>: :iterator fIter;

    I have populated the list successfully, and now I want to sort the
    list on the netTime element. All examples of the list.sort I've found
    work only with scalars or single-element structures. How do I do this
    with my data? TIA

  • Juha Nieminen

    #2
    Re: Sorting a list Structure

    Mike Copeland wrote:
    I'm confused about how to sort a list on an element of the list's
    structure. Here is my data definition:
    >
    struct Finishes // computed Finish information
    {
    long netTime;
    int bibNumber;
    } fWork;
    typedef list<FinishesFI NISHES;
    FINISHES finishInfo;
    list<Finishes>: :iterator fIter;
    >
    I have populated the list successfully, and now I want to sort the
    list on the netTime element. All examples of the list.sort I've found
    work only with scalars or single-element structures. How do I do this
    with my data? TIA
    You either define an operator<() in your Finishes struct, or you give
    a comparator function to the std::list::sort () function as a parameter.

    If comparing objects of type 'Finishes' with the < operator is
    rational, that's the easiest solution. In other words, you would do this:

    struct Finishes
    {
    long netTime;
    int bibNumber;

    bool operator<(const Finishes& rhs) const
    {
    return netTime < rhs.netTime;
    }
    };

    If the operator < does not make logical sense in the struct and you
    would want to avoid it, then you can supply a comparator to the list
    sort function:

    bool finishesCompara tor(const Finishes& lhs, const Finishes& rhs)
    {
    return lhs.netTime < rhs.netTime;
    }
    ....

    yourList.sort(f inishesComparat or);

    Comment

    • Salt_Peter

      #3
      Re: Sorting a list Structure

      On Oct 27, 5:19 pm, mrc2...@cox.net (Mike Copeland) wrote:
         I'm confused about how to sort a list on an element of the list's
      structure.  Here is my data definition:
      >
      struct Finishes               // computed Finish information
      {
              long  netTime;
              int   bibNumber;} fWork;
      >
      typedef list<FinishesFI NISHES;
              FINISHES finishInfo;
              list<Finishes>: :iterator fIter;
      >
         I have populated the list successfully, and now I want to sort the
      list on the netTime element.  All examples of the list.sort I've found
      work only with scalars or single-element structures.  How do I do this
      with my data?  TIA
      Why not use an appropriate container like std::map where netTime is
      the key and the integer its values? Elements are then sorted on
      insertion using the default comparator less<>.

      #include <iostream>
      #include <map>

      int main()
      {
      std::map< long, int finishes;
      finishes[10L] = 10;
      finishes[3L] = 3;
      finishes[2L] = 2;
      finishes[4L] = 4;
      finishes[1L] = 1;
      finishes.insert ( std::pair< long, int >(9L, 9) );


      typedef std::map< long, int >::iterator MIter;
      for( MIter miter = finishes.begin( );
      miter != finishes.end();
      ++miter )
      {
      std::cout << (*miter).first;
      std::cout << "\t";
      std::cout << (*miter).second << std::endl;
      }

      std::cout << "press Enter to EXIT\n";
      std::cin.get();
      }

      /*
      1 1
      2 2
      3 3
      4 4
      9 9
      10 10
      */

      Comment

      • Mike Copeland

        #4
        Re: Sorting a list Structure

        I have populated the list successfully, and now I want to sort the
        list on the netTime element. All examples of the list.sort I've found
        work only with scalars or single-element structures. How do I do this
        with my data? TIA
        >
        You either define an operator<() in your Finishes struct, or you give
        a comparator function to the std::list::sort () function as a parameter.
        >
        If comparing objects of type 'Finishes' with the < operator is
        rational, that's the easiest solution. In other words, you would do this:
        >
        struct Finishes
        {
        long netTime;
        int bibNumber;
        >
        bool operator<(const Finishes& rhs) const
        {
        return netTime < rhs.netTime;
        }
        };
        Yes, that works perfectly. Thanks!

        Comment

        • Mike Copeland

          #5
          Re: Sorting a list Structure

          =A0 =A0I'm confused about how to sort a list on an element of the list's
          structure. =A0Here is my data definition:

          struct Finishes =A0 =A0 =A0 =A0 =A0 =A0 =A0 // computed Finish information
          {
          =A0 =A0 =A0 =A0 long =A0netTime;
          =A0 =A0 =A0 =A0 int =A0 bibNumber;} fWork;

          typedef list<FinishesFI NISHES;
          =A0 =A0 =A0 =A0 FINISHES finishInfo;
          =A0 =A0 =A0 =A0 list<Finishes>: :iterator fIter;

          =A0 =A0I have populated the list successfully, and now I want to sort the
          list on the netTime element. =A0All examples of the list.sort I've found
          work only with scalars or single-element structures. =A0How do I do this
          with my data? =A0TIA
          >
          Why not use an appropriate container like std::map where netTime is
          the key and the integer its values? Elements are then sorted on
          insertion using the default comparator less<>.
          I don't have a need for that. My application just produces Times &
          assigns them to bibNumbers; I just need a dynamic array of such data (a
          list is fine), and then I sort the data on Time. I don't need to access
          any object members randomly, because once I've sorted the array I will
          process all objects in that sequence.

          Comment

          Working...