其他分享
首页 > 其他分享> > 多关键字排序

多关键字排序

作者:互联网

给定 n 个学生的学号(从 1 到 n 编号)以及他们的考试成绩,表示为(学号,考试成绩),请将这些学生按考试成绩降序排序,若考试成绩相同,则按学号升序排序。

样例

样例1

输入: array = [[2,50],[1,50],[3,100]]
输出: [[3,100],[1,50],[2,50]]

样例2

输入: array = [[2,50],[1,50],[3,50]]
输出: [[1,50],[2,50],[3,50]]

 

bool mycmp(vector<int> &a, vector<int> &b)
     {
        if(a[1] > b[1])
            return true;
        else if(a[1] < b[1])
            return false;
        else if(a[1] == b[1])
            {
                if(a[0] < b[0])
                    return true;
            }
        return false;
     }

class Solution {
public:
    /**
     * @param array: the input array
     * @return: the sorted array
     */
     
     
    vector<vector<int>> multiSort(vector<vector<int>> &array) {
        // Write your code here
        sort(array.begin(), array.end(), mycmp);
        return array;
    }
};

 

标签:考试成绩,return,样例,50,关键字,vector,array,排序
来源: https://blog.csdn.net/weixin_41791402/article/details/99638782