Sign in to follow this  

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

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

If you intended to correct an error in the post then please contact us.

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";
struct filestruct readintext;
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
}
while (fin >> readintext)
{
	hslist.push_back(readintext);
}
fin.close();

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

// display high score list

for(int i=0; i<hslist.size(); i++)
{
       playername = hslist[i].setting;
       score = hslist[i].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 this post


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

In your specific case:


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 this post


Link to post
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 this post


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

Quote:
Original post by Stoic
If 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 this post


Link to post
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 this post


Link to post
Share on other sites
Quote:
Original post by cdxrd
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.


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.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this