[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

Sorting a list Structure

mrc2323

10/27/2008 10:20:00 PM

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<Finishes> FINISHES;
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

16 Answers

Juha Nieminen

10/27/2008 10:29:00 PM

0

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<Finishes> FINISHES;
> 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 finishesComparator(const Finishes& lhs, const Finishes& rhs)
{
return lhs.netTime < rhs.netTime;
}
....

yourList.sort(finishesComparator);

Salt_Peter

10/28/2008 2:51:00 PM

0

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<Finishes> FINISHES;
>         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
*/

mrc2323

10/29/2008 12:38:00 PM

0

> > 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!

mrc2323

10/31/2008 8:59:00 PM

0

> > =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<Finishes> FINISHES;
> > =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.

James Kanze

11/1/2008 8:39:00 AM

0

On Oct 31, 9:58 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<Finishes> FINISHES;
> > >         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<>.

> 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.

Actually, unless you need to insert at random locations in the
middle, std::vector or std::deque are probably more appropriate;
I'd probably go with std::vector in your case, since you can do
all your insertions at the end. std::map and std::set become
interesting when you have to maintain order with ongoing
insertions.

Anyway, as others have probably already said (I haven't seen the
other responses), all you need is to define the appropriate
comparator. Something like:
struct CompareTimes
{
bool operator()(
Finishes const& lhs,
Finishes const& rhs ) const
{
return lhs.netTime < rhs.netTime ;
}
} ;
and pass an instance of this to the appropriate sort routine.

--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

rcb30

5/15/2009 2:19:00 AM

0

1. Badlands (w/Jay)
2. Radio Nowhere
3. Outlaw Pete
4. No Surrender
5. Out In The Street
6. Working On A Dream
7. Seeds
8. Johnny 99
9. Ghost of Tom Joad
collecting signs
10. Raise Your Hand
11. Thunder Road (sign request)
12. Mony Mony (sign request)
13. Waiting on a Sunny Day
14. Promised Land
15. Backstreets (sign request)
16. Kingdom of Days
17. Lonesome Day
18. The Rising
19. Born to Run
+++++++++++++++++
20. Hard Times

rcb30

5/15/2009 2:21:00 AM

0



1. Badlands (w/Jay)
2. Radio Nowhere
3. Outlaw Pete
4. No Surrender
5. Out In The Street
6. Working On A Dream
7. Seeds
8. Johnny 99
9. Ghost of Tom Joad
collecting signs
10. Raise Your Hand
11. Thunder Road (sign request)
12. Mony Mony (sign request)
13. Waiting on a Sunny Day
14. Promised Land
15. Backstreets (sign request)
16. Kingdom of Days
17. Lonesome Day
18. The Rising
19. Born to Run
+++++++++++++++++
20. Hard Times
21. Kitty's Back (sign request)

rcb30

5/15/2009 2:56:00 AM

0

Via BTX:

1. Badlands (w/Jay)
2. Radio Nowhere
3. Outlaw Pete
4. No Surrender
5. Out In The Street
6. Working On A Dream
7. Seeds
8. Johnny 99
9. Ghost of Tom Joad
collecting signs
10. Raise Your Hand
11. Thunder Road (sign request)
12. Mony Mony (sign request)
13. Waiting on a Sunny Day
14. Promised Land
15. Backstreets (sign request)
16. Kingdom of Days
17. Lonesome Day
18. The Rising
19. Born to Run
+++++++++++++++++
20. Hard Times
21. Kitty's Back (sign request)
22. Land of Hope & Dreams
23. American Land
24. Glory Days

Done.

PJ

5/15/2009 3:20:00 AM

0

If so, was the debate in the Weinberg house - if you get your haircut
I'll let you play the whole show? Just wondering...

PJ

Patrick1765

5/15/2009 4:02:00 AM

0

On May 14, 11:28?pm, "The Mighty TB"
<XOX...@unknownmailingaddress.com> wrote:
>
> > The setlist ain't the show, but this one makes me a little sad.
>
> Agreed. ?On paper, this looks to be the weakest set of the tour thus far

You can request any song you want, and you choose Thunder Road and
Backstreets ? Disappointed in Albany.