Multiple sort?

Discussion in 'C++' started by BCC, Jul 23, 2004.

  1. BCC

    BCC Guest

    Hi,

    I'm looking for some way to do a multiple sort on a vector of objects based
    on the contents of the objects. For example if my vector is of these
    objects:

    class MyClass
    {
    public:
    double value1;
    double value2;
    double value3;
    };

    So I would want to sort by each value in combination...

    Is there an existing function to do this already somewhere? Do I need to
    roll my own? Any suggestions for the fastest algorithm to use for this
    (websites or books)?

    Thanks,
    B
     
    BCC, Jul 23, 2004
    #1
    1. Advertising

  2. BCC

    Mike Wahler Guest

    "BCC" <> wrote in message
    news:pGXLc.622$...
    > Hi,
    >
    > I'm looking for some way to do a multiple sort on a vector of objects

    based
    > on the contents of the objects. For example if my vector is of these
    > objects:
    >
    > class MyClass
    > {
    > public:
    > double value1;
    > double value2;
    > double value3;
    > };
    >
    > So I would want to sort by each value in combination...
    >
    > Is there an existing function to do this already somewhere?


    There is ('std::sort', declared by <algorithm>), but you need
    to help. Define appropriate comparison operators ('operator<()'),
    each of which you pass as an argument to 'std::sort'.

    > Do I need to
    > roll my own?


    Only part of it.

    > Any suggestions for the fastest algorithm to use for this


    I'd use 'std::sort', and only pursue other options if it
    proved insufficient.

    > (websites or books)?


    www.josuttis.com/libbook

    -Mike
     
    Mike Wahler, Jul 23, 2004
    #2
    1. Advertising

  3. BCC

    rossum Guest

    On Thu, 22 Jul 2004 23:00:37 GMT, "BCC" <> wrote:

    >Hi,
    >
    >I'm looking for some way to do a multiple sort on a vector of objects based
    >on the contents of the objects. For example if my vector is of these
    >objects:
    >
    >class MyClass
    >{
    >public:
    > double value1;
    > double value2;
    > double value3;
    >};
    >
    >So I would want to sort by each value in combination...
    >
    >Is there an existing function to do this already somewhere? Do I need to
    >roll my own? Any suggestions for the fastest algorithm to use for this
    >(websites or books)?
    >
    >Thanks,
    >B
    >

    #include <algorithm>
    #include <vector>
    #include <cstdlib>

    class MyClass
    {
    public:
    double value1;
    double value2;
    double value3;

    // Friends not necessary with public data,
    // required with private data.
    friend bool less1(const MyClass& a, const MyClass& b);
    friend bool less2(const MyClass& a, const MyClass& b);
    friend bool less3(const MyClass& a, const MyClass& b);
    friend bool less_sum(const MyClass& a, const MyClass& b);
    };

    inline bool less1(const MyClass& a, const MyClass& b)
    { return a.value1 < b.value1; }

    inline bool less2(const MyClass& a, const MyClass& b)
    { return a.value2 < b.value2; }

    inline bool less3(const MyClass& a, const MyClass& b)
    { return a.value3 < b.value3; }

    inline bool lessSum(const MyClass& a, const MyClass& b)
    { return (a.value1 + a.value2 + a.value3) < (b.value1 +
    b.value2 + b.value3); }


    main() {
    std::vector<MyClass> mcVec(10);

    // Sort by value1
    std::sort(mcVec.begin(), mcVec.end(), less1);

    // Sort by value2
    std::sort(mcVec.begin(), mcVec.end(), less2);

    // Sort by value3
    std::sort(mcVec.begin(), mcVec.end(), less3);

    // Sort by sum of all three values
    std::sort(mcVec.begin(), mcVec.end(), lessSum);

    return EXIT_SUCCESS;
    } // end main()



    --

    The ultimate truth is that there is no Ultimate Truth
     
    rossum, Jul 23, 2004
    #3
    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. nobody
    Replies:
    0
    Views:
    539
    nobody
    Jun 1, 2004
  2. JerryJ
    Replies:
    11
    Views:
    1,409
    Dave Moore
    Apr 28, 2004
  3. John Black
    Replies:
    6
    Views:
    2,064
    John Harrison
    May 28, 2004
  4. Angus Comber
    Replies:
    7
    Views:
    1,164
    Richard Heathfield
    Feb 5, 2004
  5. Navin
    Replies:
    1
    Views:
    705
    Ken Schaefer
    Sep 9, 2003
Loading...

Share This Page