Your sort algorithm should take into account a zero in either position. So, if b is zero, then return true if a is not zero, or false if a is also zero†; otherwise, if a is zero, return false; otherwise (neither is zero), return the result of the normal comparison.
Here's a working example, using your data (note also the correction to the way the vectors are initialized – using a comma separator, rather than a semicolon):
#include <iostream>
#include <vector>
#include <algorithm>
int main()
{
std::vector<int> vec1{ 3, 0, 1, 0, 5 };
std::vector<int> vec2{ 3, 2, 1, 7, 5 };
auto mySort = [](const int& a, const int& b)
{
// For "short-circuiting" to work, the ORDER of the comparisons is critical!
return (b == 0 && a != 0) || (a != 0 && a < b);
};
std::sort(vec1.begin(), vec1.end(), mySort);
for (auto i : vec1) std::cout << i << " "; // 1 3 5 0 0
std::cout << std::endl;
std::sort(vec2.begin(), vec2.end(), mySort);
for (auto i : vec2) std::cout << i << " "; // 1 2 3 5 7
std::cout << std::endl;
return 0;
}
† As pointed out in the comment by Nathan Pierson, the comparator function should return false for two equal values, even if those are both zero.
std::vector. Did you perhaps want:vector<int> vec {3, 0, 1, 0, 5};?