# Sorting a list of structs

Printable View

• 06-23-2003
Jan79
Sorting a list of structs
Hi,

after using the search function for quite some time for the question i have i finally got to the point to post myself.

I have a struct defined as:

Code:

```typedef struct{   double d; //distance   int c;  //classtype }classifyprobe;```
typedef list<classifyprobe> KNNLIST;

Now i have that list and want to sort that list by its d, so i can see which classifyprobes have the least d so i can classify it. (Next neighbourhood classification)

How can i do it?

Thanks in advance!

~Jan
• 06-23-2003
Prelude
>How can i do it?
Write a comparison function and just call the sort member function of the std::list container.
Code:

`myList.sort(myCompareFunction);`
• 06-23-2003
Cat
Re: Sorting a list of structs
Quote:

Originally posted by Jan79
Hi,

after using the search function for quite some time for the question i have i finally got to the point to post myself.

I have a struct defined as:

Code:

```typedef struct{   double d; //distance   int c;  //classtype }classifyprobe;```
typedef list<classifyprobe> KNNLIST;

Now i have that list and want to sort that list by its d, so i can see which classifyprobes have the least d so i can classify it. (Next neighbourhood classification)

How can i do it?

Thanks in advance!

~Jan

1) Define your struct like this:

Code:

```struct classifyprobe{   double d; //distance   int c;  //classtype   bool operator<(const classifyprobe & c2)const{     return d < c2.d;   } };```
2) Do this:

KNNLIST.sort();
• 06-23-2003
Wledge
Write a functor (inherit from binary_function)
Code:

```struct sortClass : public binary_function<classifyprobe,classifyprobe,bool> {         result_type operator()(const first_argument_type& lhs,const second_argument_type& rhs) const {                 return lhs.d < rhs.d;         } }; ... //Call aList.sort(sortClass()); //or aList.sort(not2(sortClass()));```
• 06-23-2003
Jan79
i have now this piece of code. I use (try) a compare function for the following struct:

Code:

```typedef struct{         double d; //Distanz         int c; //Klassentyp }tknnclassify;```
and my compare function is:

Code:

```bool ccompare(tknnclassify* a, tknnclassify* b) {     return *a.d < *b.d; }```

later i just say:

Code:

```typedef list<tknnclassify> CLASSIFYLIST; CLASSIFYLIST cl; cl.sort(ccompare);```
while compiling it gives out the following error:
knnprobe.cc:413: error: request for member `d' in `a', which is of non-class
type `tknnclassify*'
knnprobe.cc:413: error: request for member `d' in `b', which is of non-class
type `tknnclassify*'

how can i fix it?

Thanks in advance!

~Jan
• 06-23-2003
elad
looks like compiler doens't believe a or b are of type tknnclassify *. Instead of this:
Code:

```typedef struct{         double d; //Distanz         int c; //Klassentyp }tknnclassify;```
I would try this:
Code:

```struct tknnclassify {   double d; //Distanz   int c; //Klassentyp }```
because I think C++ style structs are less confusing than C style structs. To do this;
Code:

```bool ccompare(tknnclassify* a, tknnclassify* b) {     return *a.d < *b.d; }```
I would call it like this:

compare(first , second);

where first and second are both of type tknnclassify * and declared like this:

tknnclassify * first;

or this;

tknnclassify * second = new tknnclassify;

and each are given a tknnclassify object to point to like this:

tknnclassify temp(1.23, 45);

first = &temp;

before being passed as parameters to compare().
• 06-23-2003
Cat
Quote:

Originally posted by Jan79
i have now this piece of code. I use (try) a compare function for the following struct:

Code:

```typedef struct{         double d; //Distanz         int c; //Klassentyp }tknnclassify;```
and my compare function is:

Code:

```bool ccompare(tknnclassify* a, tknnclassify* b) {     return *a.d < *b.d; }```

later i just say:

Code:

```typedef list<tknnclassify> CLASSIFYLIST; CLASSIFYLIST cl; cl.sort(ccompare);```
while compiling it gives out the following error:
knnprobe.cc:413: error: request for member `d' in `a', which is of non-class
type `tknnclassify*'
knnprobe.cc:413: error: request for member `d' in `b', which is of non-class
type `tknnclassify*'

how can i fix it?

Thanks in advance!

~Jan

1) Don't use the C-stye structs, as mentioned.
2) You are making a list of tknnclassify *objects* but your comparison uses *pointers*; this is no good. Do:

Code:

```bool ccompare(tknnclassify& a, tknnclassify& b) {     return a.d < b.d; }```
You will also need to tell it that you're passing a function pointer, not a functor object. So:

cl.sort(ptr_fun(ccompare));
• 06-23-2003
Zach L.
By the way, std::ptr_fun is in <functional>
• 06-24-2003
Jan79
okay, i did that.

now while compiling i get a strange error i dont understand:

i use the following structs and methods:

Code:

``` typedef struct{         double d; //Distanz         int c; //Klassentyp }tknnclassify; using namespace std; typedef list<tfingertip> FINGERLIST; typedef list<tknnclassify> CLASSIFYLIST; //prototypes bool ccompare(tknnclassify, tknnclassify); int kclassify(int, int, int); bool ccompare(tknnclassify& a, tknnclassify& b) {     return a.d < b.d; } int kclassify(int rx, int ry, int k){ CLASSIFYLIST cl; double dist; int z = 1; int f1 = 0; int f2 = 0; int f3 = 0; int f4 = 0; int f5 = 0; typedef FINGERLIST::const_iterator fliter;         for(fliter i = flist.begin();i != flist.end();++i){                 dist = computeD(rx, ry, i->x, i->y);                 tknnclassify tkc = {dist, i->c};                 cl.push_back(tkc); } cl.sort(ccompare); . . . } //i know this method is dumb progged double computeD(int rx, int ry, int listx, int listy){ double ret; double tx; double ty; double tt; double p1; double p2; tx = rx - listx; ty = ry - listy; p1 = pow(tx, 2); p2 = pow(ty, 2); tt = p1 + p2; ret = sqrt(tt); return ret; }```
When i go for compiling then it says:

/tmp/ccsxkW2X.o(.text+0x1819): In function `kclassify(int, int, int)':
: undefined reference to `ccompare(tknnclassify, tknnclassify)'
collect2: ld returned 1 exit status

Who can help me there?

Thanks in advance!
~Jan
• 06-24-2003
Jan79
got it, had to write as prototype

bool ccompare(tknnclassify&, tknnclassify&);

~Jan