Public Group

# How can I sort a vector of structs? is it possible?

This topic is 4501 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

How can I sort a vector of structs in descending order? Ive experimented with copying the score values to an array, and trying to use sort() on it, but that is proving rather complicated. Are there any ways out there to sort a vector like this or would I be better off putting this into say a two dimensional array and sorting it? Relevant code is: Im using c++ and allegro...
struct filestruct
{
std::string setting;
int value;
};

//---------------------------------------------------

std::istream& operator >> (std::istream& str_in, filestruct& d)
{
str_in >> d.setting;
str_in >> d.value;
return (str_in);
}

//---------------------------------------------------

char filename[20] = "highscores.dat";
std::vector<filestruct> hslist;

std::string playername;
int score;

int mode = (std::ios::in | std::ios::binary);

std::fstream fin(filename, mode);
if(!fin)
{
// error out code here
}
{
}
fin.close();

//---------------------------------------------------

// display high score list

for(int i=0; i<hslist.size(); i++)
{
playername = hslist.setting;
score = hslist.value;
textprintf(buffer, gk_arial, 446,starty,BLACK, "%i) %s", i+1, playername.c_str());
}

//---------------------------------------------------

File is save in this format:

Floater_Fish
9999999
Cory
999999
Krista
888888
Dylan
777777



##### Share on other sites
You can overload the < operator and (most of) the STL sorting algorithms will work. You can also pass a comparison function-object to most of the sorting algorithms.

Quick link, there's probably better docs out there

##### Share on other sites
If you overload the '<, >, ==' operators for your struct, you should be able to use the STL algorithms on them natively.

struct filestruct{     std::string setting;     int value;     bool operator < (const filestruct &fs)     { return (value < fs.value);}     bool operator > (const filestruct &fs)     { return (value > fs.value);}     bool operator == (const filestruct &fs)     { return (value == fs.value);}};

I think this should be all you need. Good luck!

##### Share on other sites
You should be able to use std::sort

struct filestruct{	std::string setting;	int value;};bool SortHS( const filestruct& elem1, const filestruct& elem2 ){	return elem1.value > elem2.value;}void SortHS(){std::vector<filestruct> hslist;std::sort(hslist.begin(), hslist.end(), SortHS);}

That should work.

Good luck,

Allan

##### Share on other sites
Quote:
 Original post by JBourrieYou can overload the < operator and (most of) the STL sorting algorithms will work.

Quote:
 Original post by StoicIf you overload the '<, >, ==' operators for your struct, you should be able to use the STL algorithms on them natively.

Overloading < just for sorting is generally not the best solution. Here are some reasons:
• The sorting criteria might be different than the normal comparison criteria. For example, you can't sort the elements in descending order and also have a high score of 9999 be less than 10000.
• You can't use a different sorting criteria in a another sort.
• If you are going to overload <, then you should also overload >, <=, >=, ==, and != in order to be consistent.
The better solution is usually to use the predicate form of sort() as demonstrated by __ODIN__.

However in this case, you might think of high scores as numbers with names attached, so it might be a good idea to overload the comparison operators. In that case you would still have to use the predicate form for a descending sort.

##### Share on other sites
Thanks for the help here guys. JBourrie, Ill have a read through that doc later on tomorrow. For today, I think Im done coding. I think Im being kind of ambitious for a first real programming project, and its slow going, but its getting there.

Odin, Stoic I'll look into those solutions and see if that will help me.. Thanks!

----------

Edit: I just quickly popped odin's solution in, and it worked very nicely. Now its time to take a few minutes to play with it, look it over, and figure out how exactly it does work.

##### Share on other sites
Quote:
 Original post by cdxrdThanks for the help here guys. JBourrie, Ill have a read through that doc later on tomorrow. For today, I think Im done coding. I think Im being kind of ambitious for a first real programming project, and its slow going, but its getting there. Odin, Stoic I'll look into those solutions and see if that will help me.. Thanks!----------Edit: I just quickly popped odin's solution in, and it worked very nicely. Now its time to take a few minutes to play with it, look it over, and figure out how exactly it does work.

The sort function takes iterators (basically pointers) to the beginning and end of the data you want sorted. The third parameter is a function pointer that specifies how you want the data sorted. The nice thing about this method is you can create multiple sorting functions and use an appropriate one: Possibly ascending, descending, or something else.

1. 1
2. 2
3. 3
Rutin
22
4. 4
frob
18
5. 5

• 33
• 13
• 10
• 10
• 11
• ### Forum Statistics

• Total Topics
632566
• Total Posts
3007111

×