Question about the sort comparator

  • -4
    static bool comp(int a, int b)
        const char *ptr_a, *ptr_b;
        ptr_a = to_string(a).c_str();
        ptr_b = to_string(b).c_str();
        if (strcmp(ptr_a, ptr_b) > 0)
            return true;
            return false;
    string largestNumber(vector<int> &num) {
        string res;
        sort(num.begin(), num.end(), comp);
        for (int i = 0; i < num.size(); i++)
            res += to_string(num[i]);
        return res;
    int main(int argc, const char * argv[])
        vector<int> num = {1, 2};
        string str = largestNumber(num);
        cout << str;

    Hi, all,

    When I run the above code using G++ compiler, the output is "21". However, the output from leetcode is "12". Would anyone help to give some comments?

    Thanks a lot.

Log in to reply

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.