Connecting Tech Pros Worldwide Help | Site Map

Multiple sort?

 
LinkBack Thread Tools Search this Thread
  #1  
Old July 22nd, 2005, 04:46 PM
BCC
Guest
 
Posts: n/a
Default Multiple sort?

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



  #2  
Old July 22nd, 2005, 04:46 PM
Mike Wahler
Guest
 
Posts: n/a
Default Re: Multiple sort?


"BCC" <bryan@akanta.com> wrote in message
news:pGXLc.622$AY5.226@newssvr21.news.prodigy.com. ..[color=blue]
> Hi,
>
> I'm looking for some way to do a multiple sort on a vector of objects[/color]
based[color=blue]
> 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?[/color]

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'.
[color=blue]
> Do I need to
> roll my own?[/color]

Only part of it.
[color=blue]
> Any suggestions for the fastest algorithm to use for this[/color]

I'd use 'std::sort', and only pursue other options if it
proved insufficient.
[color=blue]
> (websites or books)?[/color]

www.josuttis.com/libbook

-Mike


  #3  
Old July 22nd, 2005, 04:48 PM
rossum
Guest
 
Posts: n/a
Default Re: Multiple sort?

On Thu, 22 Jul 2004 23:00:37 GMT, "BCC" <bryan@akanta.com> wrote:
[color=blue]
>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
>[/color]
#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
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,840 network members.