Print the absolute sorted array. See the sample output for clarification.
input: arr = [2, -7, -2, -2, 0]
output: [0, -2, -2, 2, -7]
Now I am using a lambda function as a comparator for STL std::sort but it's not giving correct answer; a help would be appreciated.
Code:
vector<int> absSort(const vector<int>& at)
{
vector <int> arr = at;
sort(arr.begin(), arr.end(), [&](const int a, const int b){
if (abs(a) < abs(b)) return -1;
if (abs(a) > abs(b)) return 1;
if (a < b) return -1;
if (a > b) return 1;
return 0;
});
return arr;
}