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 ?

Recommended Answers

All 5 Replies

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

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

how do you create one ?

Im trying to sort objects by their z

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 < .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.