Custom sorting literals in C++

Sorting is a common operation in programming, and most programming languages provide built-in functions or libraries to perform this task efficiently. However, there may be cases where the default sorting behavior does not meet our requirements. In such situations, it becomes necessary to implement custom sorting logic to achieve the desired result.

One such scenario is when sorting literals in C++. By default, C++ sorts literals such as numbers and strings based on their natural order. However, there may be cases where we need to sort literals based on a custom criteria or rules.

In this blog post, we will discuss how to implement custom sorting logic for literals in C++. We will explore two common scenarios: sorting numbers in descending order and sorting strings based on their lengths.

Sorting Numbers in Descending Order

Let’s say we have an array of integers, and we want to sort them in descending order instead of the default ascending order. To achieve this, we can define a custom comparison function and pass it as a parameter to the sorting algorithm.

#include <iostream>
#include <algorithm>
#include <vector>

bool descendingCompare(int a, int b) {
    return a > b;  // Compare in descending order
}

int main() {
    std::vector<int> numbers = {5, 2, 8, 3, 1};
    
    std::sort(numbers.begin(), numbers.end(), descendingCompare);
    
    // Print the sorted numbers
    for (int num : numbers) {
        std::cout << num << " ";
    }
    
    return 0;
}

In the code snippet above, we define a function descendingCompare that compares two integers in descending order. We then pass this function as the third parameter to the std::sort function, which will sort the numbers vector accordingly.

The output of this program will be: 8 5 3 2 1, which is the sorted array in descending order.

Sorting Strings Based on Length

Now let’s consider another scenario where we have an array of strings, and we want to sort them based on their lengths. Again, we can achieve this by defining a custom comparison function and passing it to the sorting algorithm.

#include <iostream>
#include <algorithm>
#include <vector>
#include <string>

bool lengthCompare(const std::string& a, const std::string& b) {
    return a.length() < b.length();  // Compare based on string length
}

int main() {
    std::vector<std::string> words = {"apple", "banana", "cat", "dog", "elephant"};
    
    std::sort(words.begin(), words.end(), lengthCompare);
    
    // Print the sorted words
    for (const std::string& word : words) {
        std::cout << word << " ";
    }
    
    return 0;
}

In the code above, we define a function lengthCompare that compares two strings based on their lengths. We pass this function as the third parameter to the std::sort function, which will sort the words vector accordingly.

The output of this program will be: cat dog apple banana elephant, which is the sorted array based on string length.

Conclusion

Custom sorting literals in C++ allows us to tailor the sorting logic to fit our specific requirements. By defining custom comparison functions, we can sort numbers in descending order, sort strings based on length, or any other custom criteria we need.

Understanding how to implement custom sorting logic in C++ can greatly enhance our programming capabilities and allow us to tackle a wider range of problems efficiently.

#references