473,395 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Sorting STL lists

Hi,

I need some help with STL lists.

I've a list with pointers to some objects,

let's say std::list< ObjectA* myList.

The objects of the class ObjectA all have a
function "int getIntValue()". What I need
now is to sort all elements of "myList" by the
return value of "getIntValue()", i.e. after sorting
the first element in the list will be the one with
the smallest return value of "getIntValue()" ... and
the last obviously with the largest.

Could you give me some hints how to do that?

Regards,
Tim
Jul 14 '07 #1
6 2167
On 2007-07-14 16:29, Tim Frink wrote:
Hi,

I need some help with STL lists.

I've a list with pointers to some objects,

let's say std::list< ObjectA* myList.

The objects of the class ObjectA all have a
function "int getIntValue()". What I need
now is to sort all elements of "myList" by the
return value of "getIntValue()", i.e. after sorting
the first element in the list will be the one with
the smallest return value of "getIntValue()" ... and
the last obviously with the largest.

Could you give me some hints how to do that?
You have to create a comparator function, something like

bool cmp(const ObjectA* a, const ObjectA* b)
{
return a->getIntValue() < b->getIntValeu();
}
and then use

std::sort(list.begin(), list.end(), cmp);

--
Erik Wikström
Jul 14 '07 #2
Tim Frink wrote:
I've a list with pointers to some objects,

let's say std::list< ObjectA* myList.

The objects of the class ObjectA all have a
function "int getIntValue()". What I need
now is to sort all elements of "myList" by the
return value of "getIntValue()", i.e. after sorting
the first element in the list will be the one with
the smallest return value of "getIntValue()" ... and
the last obviously with the largest.

#include <list>
#include <cassert>
#include <iostream>
#include <ostream>

struct Object {

int i;

Object ( int k )
: i ( k )
{}

int getIntValue ( void ) {
return ( i );
}

};

bool ptr_compare( Object * lhs, Object * rhs ) {
assert( lhs != 0 );
assert( rhs != 0 );
return ( lhs->getIntValue() < rhs->getIntValue() );
}

typedef std::list< Object* PtrList;

int main ( void ) {
PtrList ptr_list;
ptr_list.push_back( new Object ( 4 ) );
ptr_list.push_back( new Object ( 3 ) );
ptr_list.push_back( new Object ( 1 ) );
ptr_list.push_back( new Object ( 7 ) );
ptr_list.push_back( new Object ( 4 ) );
ptr_list.push_back( new Object ( 5 ) );
for ( PtrList::const_iterator iter = ptr_list.begin();
iter != ptr_list.end(); ++ iter ) {
std::cout << (*iter)->getIntValue() << '\n';
}
std::cout << '\n';

ptr_list.sort( &ptr_compare ); // !!!!!!!!!!!!!!!!!!!!

for ( PtrList::const_iterator iter = ptr_list.begin();
iter != ptr_list.end(); ++ iter ) {
std::cout << (*iter)->getIntValue() << '\n';
}
}

Best

Kai-Uwe Bux
Jul 14 '07 #3
Erik Wikström wrote:
On 2007-07-14 16:29, Tim Frink wrote:
>Hi,

I need some help with STL lists.

I've a list with pointers to some objects,

let's say std::list< ObjectA* myList.

The objects of the class ObjectA all have a
function "int getIntValue()". What I need
now is to sort all elements of "myList" by the
return value of "getIntValue()", i.e. after sorting
the first element in the list will be the one with
the smallest return value of "getIntValue()" ... and
the last obviously with the largest.

Could you give me some hints how to do that?

You have to create a comparator function, something like

bool cmp(const ObjectA* a, const ObjectA* b)
{
return a->getIntValue() < b->getIntValeu();
}
and then use

std::sort(list.begin(), list.end(), cmp);
I think, std::sort() needs random access iterators. You want:

list.sort( cmp );
Best

Kai-Uwe Bux

Jul 14 '07 #4
Tim Frink wrote:
Hi,

I need some help with STL lists.

I've a list with pointers to some objects,

let's say std::list< ObjectA* myList.

The objects of the class ObjectA all have a
function "int getIntValue()". What I need
now is to sort all elements of "myList" by the
return value of "getIntValue()", i.e. after sorting
the first element in the list will be the one with
the smallest return value of "getIntValue()" ... and
the last obviously with the largest.

Could you give me some hints how to do that?

Regards,
Tim
As stated earlier you need a comparator function to use with
std::sort(). Myself I created a template to handle iterator type
comparisons like that below

template <typename T>
inline bool DRefCompare(T i1,T i2)
{
return *i1 < *i2;
}

class Compartment
{
public:
typedef std::vector<MyObjectvector;
typedef std::vector<vector::iteratorivector;
typedef std::vector<vector::const_iteratorcivector;

bool operator < (const MyObject &inp) const;
/*
Rest of the definition here
*/
};

MyObject::ivector viCmp;
/*
Other code here
Create sorted list of MyObject iterators
*/
MyObject::vector::iterator iC = vCmp.begin();
do
{
viCmp.push_back(iC);
}
while (++iC != vCmp.end());

std::sort(viCmp.begin(),viCmp.end(),&DRefCompare<M yObject::vector::const_iterator>);

Naturally this relies on the operator < () exists for type T, so changes
to make it more general might be more suitable

JB
Jul 14 '07 #5
bnonaj wrote:
Tim Frink wrote:
>Hi,

I need some help with STL lists.

I've a list with pointers to some objects,

let's say std::list< ObjectA* myList.

The objects of the class ObjectA all have a
function "int getIntValue()". What I need
now is to sort all elements of "myList" by the
return value of "getIntValue()", i.e. after sorting
the first element in the list will be the one with
the smallest return value of "getIntValue()" ... and
the last obviously with the largest.

Could you give me some hints how to do that?

Regards,
Tim

As stated earlier you need a comparator function to use with
std::sort(). Myself I created a template to handle iterator type
comparisons like that below

template <typename T>
inline bool DRefCompare(T i1,T i2)
{
return *i1 < *i2;
}

class Compartment
{
public:
typedef std::vector<MyObjectvector;
typedef std::vector<vector::iteratorivector;
typedef std::vector<vector::const_iteratorcivector;

bool operator < (const MyObject &inp) const;
/*
Rest of the definition here
*/
};

MyObject::ivector viCmp;
/*
Other code here
Create sorted list of MyObject iterators
*/
MyObject::vector::iterator iC = vCmp.begin();
do
{
viCmp.push_back(iC);
}
while (++iC != vCmp.end());

std::sort(viCmp.begin(),viCmp.end(),&DRefCompare<M yObject::vector::const_iterator>);
Naturally this relies on the operator < () exists for type T, so changes
to make it more general might be more suitable

JB
That would be using std::list::sort for std::list types as pointed out
earlier

JB
Jul 14 '07 #6
On Sat, 14 Jul 2007 16:53:50 +0200, Kai-Uwe Bux wrote:
>
ptr_list.sort( &ptr_compare ); // !!!!!!!!!!!!!!!!!!!!

for ( PtrList::const_iterator iter = ptr_list.begin();
iter != ptr_list.end(); ++ iter ) {
std::cout << (*iter)->getIntValue() << '\n';
}
}
Many thanks.

Regards,
Tim

Jul 16 '07 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

39
by: Erlend Fuglum | last post by:
Hi everyone, I'm having some trouble sorting lists. I suspect this might have something to do with locale settings and/or character encoding/unicode. Consider the following example, text...
10
by: Philippe C. Martin | last post by:
Hi, I'm looking for an easy algorithm - maybe Python can help: I start with X lists which intial sort is based on list #1. I want to reverse sort list #1 and have all other lists sorted...
2
by: Kakarot | last post by:
I'm gona be very honest here, I suck at programming, *especially* at C++. It's funny because I actually like the idea of programming ... normally what I like I'm atleast decent at. But C++ is a...
7
by: Foodbank | last post by:
Hi everyone. I'm having trouble with this radix sorting program. I've gotten some of it coded except for the actual sorting :( The book I'm teaching myself with (Data Structures Using C and...
25
by: Dan Stromberg | last post by:
Hi folks. Python appears to have a good sort method, but when sorting array elements that are very large, and hence have very expensive compares, is there some sort of already-available sort...
4
by: FBM | last post by:
Hi, I am working on a program that simulates one of the elements of ATM. The simulation stores events which occurs every some milliseconds for a certain amount of time. Every time that an event...
2
by: Susan.Adkins | last post by:
Alright, I must say that the problem my teacher has given us to do has royally annoyed me. We are to take a letter *txt file* and then read in the words. Make 2 seperate lists *even and odd* for...
16
by: Kittyhawk | last post by:
I would like to sort an Arraylist of objects on multiple properties. For instance, I have a Sort Index property and an ID property (both integers). So, the results of my sort would look like this:...
20
by: martin-g | last post by:
Hi. Mostly I program in C++, and I'm not fluent in C# and .NET. In my last project I began to use LinkedList<and suddenly noticed that can't find a way to sort it. Does it mean I must implement...
7
by: apotheos | last post by:
I can't seem to get this nailed down and I thought I'd toss it out there as, by gosh, its got to be something simple I'm missing. I have two different database tables of events that use different...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.