Javatpoint Logo
Javatpoint Logo

Custom sort string in C++

A "custom sort string" refers to a specific way of sorting strings that deviates from the standard lexicographical (dictionary) order. In custom sorting, you define your order for characters or substrings within strings. This custom order can be based on various criteria, such as specific character sequences or a predefined order of characters.

Custom sorting in C++ is a powerful feature that allows you to sort elements in a way that is not based solely on their natural order, like the default sorting for integers or strings. Instead, you can define your criteria for how elements should be ordered. It is often required when you're working with complex data structures or custom objects that don't have a built-in comparison operator.

Custom string sorting is often used in scenarios when you want to sort strings in a way that makes sense for a particular application or context.

Why Use Custom String Sorting?

Non-Alphabetical Sorting: In many cases, you might want to sort strings that don't contain only letters of the alphabet. For example, sorting filenames that include numbers, special characters, and text can be challenging with standard sorting.

Custom Prioritization: Sometimes, certain strings should come first in the sorted list regardless of their lexicographical order. It is common in user interfaces when you want to prioritize certain options or categories.

Sorting by Specific Criteria: In some applications, you may need to sort strings based on specific criteria like importance, relevance, or frequency. Custom sorting allows you to define these criteria.

Custom sorting using function objects:

Custom sorting using function objects, also known as functors, is a technique in C++ that allows you to define your sorting criteria when working with elements that don't have a natural ordering. It is particularly useful when sorting complex data structures or custom objects where the default sorting order (e.g., alphabetical or numerical) doesn't apply. In this explanation, we'll delve into the details of custom sorting using function objects in C++.

Basics of Custom Sorting with Function Objects:

Function Objects (Functors): In C++, a function object, or functor, is an object that acts like a function. Functors are created by overloading the operator() within a class or structure. This operator allows you to define the comparison logic that will be used for sorting.

std::sort Algorithm: The std::sort function from the C++ Standard Library (STL) is used to sort elements in a range. By default, std::sort uses the less-than (<) operator to compare elements for sorting. However, you can provide your custom comparison logic by passing a custom functor to std::sort.

Creating a Custom Functor for Sorting:

Here's a step-by-step guide on how to create and use a custom functor for sorting:

Define the Functor Class or Struct: Create a class or struct that overloads the operator() to define the sorting criteria. This operator should return true if the first element should come before the second in the sorted order and false otherwise.

Replace T with the data type of the elements you want to sort.

Instantiate the Functor: Create an instance of the custom comparator functor.

Use std::sort with the Custom Functor: Call std::sort for passing the range of elements you want to sort and the custom comparator functor as an argument.

Program:

Let's take an example to demonstrate the custom sort string in C++:

Output:

David (22 years) Bob (25 years) Alice (30 years) Charlie (35 years)

Explanation:

  • In this example, we include necessary C++ Standard Library headers, such as <iostream> to enable input and output operations, <vector> header to create dynamic arrays (vectors), and use the std::sort function for sorting.
  • After that, we define a custom class called Person to represent individuals. This class has two attributes: name, a string representing the person's name, and age, an integer representing the person's age. The class also has a constructor that initializes these attributes when a Person object is created.
  • Next, we define a custom comparison functor called AgeComparator. A functor is an object that acts like a function. In this case, AgeComparator overloads the operator() to define the custom comparison logic. The logic compares two Person objects based on their ages (person1.age and person2.age) and returns true if person1 should come before person2 in the sorted order and false otherwise.

In the main() function:

  • We create a vector named people that stores instances of the Person class. These instances represent individuals with names and ages.
  • We create an instance of the custom comparison functor AgeComparator called ageComparator. This functor will be used to define the sorting order.
  • We use the std::sort function to sort the vector of Person objects people. The std::sort function takes three arguments: the beginning and ending iterators of the range to be sorted (people.begin() and people.end()) and the custom comparison functor ageComparator. It tells std::sort to use the AgeComparator functor to determine the sorting order based on age.
  • Finally, we Display the sorted result by iterating over the people vector and printing each person's name and age to the console.
  • The output of the code will be a list of Person objects sorted by age, with their names and ages displayed in ascending order of age.

Complexity Analysis:

Time Complexity:

  • Creating Person Objects: Creating instances of the Person class and initializing their attributes has a time complexity of O(1) for each object. If there are 'n' persons in the people vector, the total time complexity is O(n) for creating and initializing the objects.
  • Sorting (std::sort): In this code, the most significant time complexity factor is the sorting operation using std::sort. The time complexity of std::sort typically depends on the sorting algorithm used. In most STL implementations, it uses an efficient sorting algorithm like introsort (a combination of quicksort, heapsort, and insertion sort).
  • On average, quicksort (which is part of introsort) has an average-case time complexity of O(n log n).
  • In the worst case, quicksort can degrade to O(n^2), but the likelihood of this happening is low, and introsort takes measures to prevent this.
  • In practice, std::sort is highly optimized and often outperforms O(n log n) algorithms for small to moderately-sized inputs.
  • So, for the sorting step, the overall time complexity is usually O(n log n) on average and in the worst case. Still, it could be faster in practice due to optimizations.

Space Complexity:

  • Vector people: The space complexity of the people vector is O(n), where 'n' is the number of Person objects stored in the vector. This accounts for the memory used to store the name and age attributes of each person.
  • Custom Comparison Functor (AgeComparator): The space complexity of the custom comparison functor AgeComparator is negligible. It only stores the comparison logic, which is a very small amount of memory.
  • Local Variables and Constants: The space complexity of local variables like ageComparator and constants used in the code is also negligible and doesn't depend on the input size.

Custom sorting using lambda functions:

Custom sorting using lambda functions in C++ is a technique that allows you to define custom sorting criteria inline without the need for separate comparator functions or functors. Lambdas are anonymous functions that can capture variables from their enclosing scope, making them concise and convenient for small, one-time sorting operations. In this explanation, we'll explore how to use lambda functions for custom sorting in C++.

Basics of Custom Sorting with Lambda Functions:

Lambda Functions: A lambda function is a compact, anonymous function that can be defined inline. Lambda functions allow you to specify custom behaviour without the need to define a separate named function or functor.

std::sort Algorithm: Just like custom sorting with function objects, you can use lambda functions with the std::sort function from the C++ Standard Library to sort a range of elements based on your custom criteria.

Creating a Lambda Function for Sorting:

Here's a step-by-step guide on how to create and use a lambda function for custom sorting:

Lambda Syntax: Lambda functions are defined using the following syntax:

capture_clause: It specifies which variables from the enclosing scope should be captured and made available inside the lambda function.

parameter_list: It specifies the function's parameters.

return_type: It specifies the return type.

Custom Logic: Inside the lambda's function body, you can define your custom comparison logic.

Use std::sort with the Lambda: Call std::sort for passing the range of elements you want to sort and the lambda function as an argument.

Program:

Let's take an example to demonstrate the custom sort string with lambda in C++:

Output:

banana cherry apple date fig

Explanation:

In this example, we include necessary C++ Standard Library headers using #include.

Inside the main() function:

  • We create a vector of strings named words and initialize it with five string elements.
  • After that, we use the std::sort function to sort the words vector. A lambda function is used as the custom comparison logic to compare strings by their lengths in descending order.
  • The sorted result is displayed to the console using a for loop, and each word is printed.

Complexiy Analysis:

Time Complexity:

The time complexity of the code is dominated by the sorting operation and is approximately O(n log n) on average for sorting the strings by length in descending order.

Space Complexity:

The space complexity of the code is primarily determined by the size of the words vector, resulting in a space complexity of O(n). The time complexity is dominated by the sorting operation, which has an average-case time complexity of O(n log n). Still, it could be faster in practice due to optimizations provided by the std::sort algorithm.

Custom sorting with complex Data Structures:

Custom sorting with complex data structures in C++ involves arranging elements within data structures, such as arrays, vectors, or custom-defined classes, based on specific criteria or attributes. It allows you to sort data according to your unique requirements rather than relying on the default sorting order of elements. Here's an explanation of how custom sorting with complex data structures works:

Custom Sorting Process:

Define a Data Structure: You start by defining the complex data structure that holds your data. It can be an array, vector, or a custom class that encapsulates multiple attributes.

Custom Comparison Logic: It determine the criteria for sorting your data. You'll need to create a custom comparison function, functor, or lambda function that defines how elements should be compared. The comparison logic typically focuses on one or more attributes of the data structure.

Use Sorting Algorithms: Using the custom comparison logic, apply sorting algorithms, such as std::sort for vectors or custom sorting routines for arrays. The sorting algorithm rearranges the elements according to your criteria.

Display or Use Sorted Data: After sorting, you can display the sorted data or use it for further processing in your program.

Program:

Let's take an example to demonstrate the custom sort string with complex data structure in C++:

Output:

Charlie (19 years) Alice (20 years) Bob (22 years)

Explanation:

In this example, we define a custom class named Person to represent individuals, with two attributes: name (a string) and age (an integer). The class has a constructor that initializes these attributes when a Person object is created.

In the main() function:

  • In this function, we create a vector of Person objects named people and initialize it with three Person instances, each with a name and age.
  • We use the std::sort function to sort the people vector. We provide a lambda function as the custom comparison logic. The lambda function takes two Person objects (person1 and person2) as parameters and compares them based on their name attribute using the < operator. This lambda function defines the sorting order in ascending order of names.
  • After sorting, the people vector contains Person objects sorted by their names in alphabetical order.
  • We iterate through the sorted people vector using a range-based for loop and display each person's name and age to the console.

Complexity Analysis:

Time Complexity:

The time complexity of the code is dominated by the sorting operation and is approximately O(n log n) on average for sorting the people vector by name.

Space Complexity:

The space complexity of the code is primarily determined by the size of the people vector, resulting in a space complexity of O(n).







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA