Answers for "c++ sort numbers by magnitude/absolute value"

C++
0

c++ sort numbers by magnitude/absolute value

// sort numbers by their magnitude value
bool sortByAbsolute(int a, int b) {
    return abs(a) < abs(b);
}

void find(std::vector<int> &nums, std::vector<int> &sorted) {
    std::sort(nums.begin(), nums.end(), sortByAbsolute);
}
Posted by: Guest on September-30-2021

Browse Popular Code Answers by Language