0

hello my question is, How do you sort a vector containing a bunch of game_object* 's ? You can use STD's algorithm.sort() somehow ... right ?

4
Contributors
5
Replies
6
Views
7 Years
Discussion Span
Last Post by mitrmkar
0

Yes, you could use sort( ), provided you provide a comparison function.

how do you create one ?

Im trying to sort objects by their z

1

how do you create one ?

Im trying to sort objects by their z

You might have something as simple as

// Compares two game objects by 'z'
bool game_obj_compare(const game_obj * lhs, const game_obj * rhs)
{
  return lhs->z < rhs->z;
}

// To sort a vector of game_obj pointers ...
std::sort(v.begin(), v.end(), game_obj_compare);

Since the vector stores pointers, you cannot use the

std::sort(v.begin(), v.end());

version, which uses the overloaded operator < .

Edited by mitrmkar: bool game_obj_compare(...)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.