Go Back   Velocity Reviews > Newsgroups > C++
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

C++ - Multiple sort?

 
Thread Tools Search this Thread
Old 07-23-2004, 12:00 AM   #1
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




BCC
  Reply With Quote
Old 07-23-2004, 02:12 AM   #2
Mike Wahler
 
Posts: n/a
Default Re: Multiple sort?

"BCC" <> wrote in message
newsGXLc.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
  Reply With Quote
Old 07-23-2004, 09:32 PM   #3
rossum
 
Posts: n/a
Default Re: Multiple sort?
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
  Reply With Quote
Reply


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

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

Similar Threads
Thread Thread Starter Forum Replies Last Post
HELP!! anyone ??can help me about my project "quick sort implemented with shell sort? comsciepartner General Help Related Topics 0 10-06-2008 02:02 PM
Multiple DVD editions Bernie Woodham DVD Video 13 04-04-2006 08:21 PM
Multiple Titles with DVD Shrink 3.2 Bob DVD Video 2 10-28-2005 02:34 AM
Multiple DiVX/VCDs on a DVD Sabian Smith DVD Video 11 10-02-2004 01:46 PM
Multiple DiVX/VCDs on a DVD Sabian Smith DVD Video 0 09-29-2004 04:23 AM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46