Custom Comparator for Sorting

Custom Comparator for Sorting

Hard C++ STL 35 views
Explanation Complexity

Problem Statement

Sort vector of pairs by second element.
Real Life: Sorting students by marks, not by name.

Input Format

An integer n (number of pairs)
Then n pairs:
(first, second)

Output Format

Pairs sorted in ascending order by second element.

Example

4
(Alice, 85)
(Bob, 92)
(Charlie, 78)
(David, 85)
(conceptual input; internally stored as pair<int,int> or pair<string,int>)
Charlie 78
Alice 85
David 85
Bob 92

Constraints

• Use vector

• Sorting must be based on second value

• Stable order not required

Concept Explanation

Sometimes sorting is needed not by the first value,
but by the second value (like sorting students by marks instead of names).

Step-by-Step Explanation

1.Store data in a vector of pair.

2.Define a custom comparison function.

3.In the comparison function:

• Compare pair1.second with pair2.second.

4.Use sort() function with this comparator.

5.After sorting, pairs are arranged by second value.

6.Print all pairs in sorted order.

Concept Explanation

Sometimes sorting is needed not by the first value,
but by the second value (like sorting students by marks instead of names).

Step-by-Step Explanation

1.Store data in a vector of pair.

2.Define a custom comparison function.

3.In the comparison function:

• Compare pair1.second with pair2.second.

4.Use sort() function with this comparator.

5.After sorting, pairs are arranged by second value.

6.Print all pairs in sorted order.

Input / Output Format

Input Format
An integer n (number of pairs)
Then n pairs:
(first, second)
Output Format
Pairs sorted in ascending order by second element.
Constraints
• Use vector

• Sorting must be based on second value

• Stable order not required

Examples

Input:
4 (Alice, 85) (Bob, 92) (Charlie, 78) (David, 85) (conceptual input; internally stored as pair<int,int> or pair<string,int>)
Output:
Charlie 78 Alice 85 David 85 Bob 92

Example Solution (Public)

C++
bool compareBySecond(pair<string, int> a, pair<string, int> b) {
    return a.second > b.second;  // Descending order
}

void stl_q11_custom_sort() {
    vector<pair<string, int>> students;
    students.push_back(make_pair("Raj", 85));
    students.push_back(make_pair("Priya", 92));
    students.push_back(make_pair("Amit", 78));
    
    sort(students.begin(), students.end(), compareBySecond);
    
    cout << "Students sorted by marks:" << endl;
    for(auto s : students) {
        cout << s.first << " : " << s.second << endl;
    }
}

Official Solution Code

bool compareBySecond(pair<string, int> a, pair<string, int> b) {
    return a.second > b.second;  // Descending order
}

void stl_q11_custom_sort() {
    vector<pair<string, int>> students;
    students.push_back(make_pair("Raj", 85));
    students.push_back(make_pair("Priya", 92));
    students.push_back(make_pair("Amit", 78));
    
    sort(students.begin(), students.end(), compareBySecond);
    
    cout << "Students sorted by marks:" << endl;
    for(auto s : students) {
        cout << s.first << " : " << s.second << endl;
    }
}
Please login to submit solutions.
Editor
Output

                                        
Please login to submit solutions.