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
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();