Solved

# How do I sort a vector of struct using different methods ?

Posted on 2005-04-21
347 Views
Hi experts,

typedef struct{
int tail ;
// ... blah
} Animal ;

I want to sort vector<Animal>. In some cases I want to sort by head; but in other cases, I want to sort by tail ....

so how do I do this ? should I write a predicator ? if so, how to do it ?

many thanks !
0
Question by:meow00

LVL 50

Expert Comment

0

LVL 50

Expert Comment

0

LVL 50

Accepted Solution

Because you want to sort on different element you will need to write a predicate function for each method you want to use. Refer to the sample code below

Eg.

#include "windows.h"
#include <tchar.h>
#include "stdio.h"
#include <vector>
#include <algorithm>

using namespace std ;

typedef struct
{
int tail ;
} ANIMAL ;

bool SortByHeadDesc ( ANIMAL elem1, ANIMAL elem2 )
{
}

bool SortByTailDesc ( ANIMAL elem1, ANIMAL elem2 )
{
return elem1.tail > elem2.tail ;
}

bool SortByHeadAsc ( ANIMAL elem1, ANIMAL elem2 )
{
}

bool SortByTailAsc ( ANIMAL elem1, ANIMAL elem2 )
{
return elem1.tail < elem2.tail ;
}

int _tmain(int argc, _TCHAR* argv[])
{
vector<ANIMAL> vec ;
ANIMAL elem1 = {1,30} ;
ANIMAL elem2 = {2,20} ;
ANIMAL elem3 = {7,90} ;
ANIMAL elem4 = {9,15} ;
ANIMAL elem5 = {3,10} ;

vec.push_back ( elem1  ) ;
vec.push_back ( elem2  ) ;
vec.push_back ( elem3  ) ;
vec.push_back ( elem4  ) ;
vec.push_back ( elem5  ) ;

vector<ANIMAL>::iterator it ;

printf ( "Sort by head (first element) Descending\n" ) ;
sort ( vec.begin(), vec.end(), SortByHeadDesc ) ;
for ( it = vec.begin(); it != vec.end(); it++ )
{
printf ( "%d, %d\n", it->head, it->tail ) ;
}

printf ( "\nSort by tail (second element) Descending\n" ) ;
sort ( vec.begin(), vec.end(), SortByTailDesc ) ;
for ( it = vec.begin(); it != vec.end(); it++ )
{
printf ( "%d, %d\n", it->head, it->tail ) ;
}

printf ( "Sort by head (first element) Ascending\n" ) ;
sort ( vec.begin(), vec.end(), SortByHeadAsc ) ;
for ( it = vec.begin(); it != vec.end(); it++ )
{
printf ( "%d, %d\n", it->head, it->tail ) ;
}

printf ( "\nSort by tail (second element) Ascending\n" ) ;
sort ( vec.begin(), vec.end(), SortByTailAsc ) ;
for ( it = vec.begin(); it != vec.end(); it++ )
{
printf ( "%d, %d\n", it->head, it->tail ) ;
}

return 0;
}

0

## Featured Post

### Suggested Solutions

Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on thâ€¦
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilationâ€¦
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.