Compartors in C++
Difference between en1 and en2, changed 93 character(s)

~~~~~↵
#include <bits/stdc++.h>↵

using namespace std;↵

bool comp(const int &a , const int&b)↵
{↵
    return ((a > 0 and b > 0) ? true : ((a == 0) ? false : true));↵
}↵


int32_t main()↵
{↵
    vector <int> a = {1 , 0 , 0 , 3 , 12};↵
    sort(a.begin() , a.end() , comp);↵
    for(int &x : a)↵
        cout << x << ", " ;↵
    cout << endl;↵
    return 0;↵
}↵

~~~~~↵


The aim is to move all the zeroes to the back keeping the relative order of all other non-zero elements intact.↵
I thought the comparator would do the job as it changes the relative order of two elements `a` and `b` if `a is equal to 0` and not in any other case, and print the final output as :↵

`0, 0, 1, 3, 12,`↵

However, it printed this : ↵

`12, 3, 1, 0, 0,`↵

Why is the comparator failing?↵



THIS REALLY HELPED : [Custom Comparators](http://https://codeforces.com/blog/entry/72525)↵

History

 
 
 
 
Revisions
 
 
  Rev. Lang. By When Δ Comment
en2 English wannbegood 2020-08-10 09:53:33 93
en1 English wannbegood 2020-08-08 12:04:56 807 Initial revision (published)