How to Sort a Vector of Custom Objects in C++? Last Updated : 01 Mar, 2024 Comments Improve Suggest changes Like Article Like Report In C++, vectors are dynamic arrays and sorting a vector of custom objects means arranging the objects in a certain order. In this article, we will learn how to sort a vector of custom objects in C++. Sorting a Vector of Custom Objects in C++To sort a vector of custom objects in C++, we can use the std::sort() function that sorts the elements in the range [first, last) into the order specified by the comparator function that compares these custom objects. If you do not want to use the custom comparator, then you may need to overload the comparison operators for the custom objects. Syntax of std::sort()sort(first_iterator, last_iterator, comparison_function);Here, first_iterator is the iterator to the beginning of the range to be sorted.last_iterator is the iterator to the end of the range to be sorted.comparison_function is a binary function that accepts two elements in the range as arguments, and returns a bool value.C++ Program to Sort a Vector of Custom ObjectsThe below example demonstrates how we can sort the vector of custom objects in C++. C++ // C++ program to illustrate how to sort a vector of custom // objects #include <algorithm> #include <iostream> #include <vector> using namespace std; // creating a custom class class MyClass { public: int data; MyClass(int a) : data(a) { } // Overloading < operator bool operator<(const MyClass& obj) const { return data < obj.data; } }; int main() { // Vector of custom objects vector<MyClass> vec; vec.push_back(MyClass(30)); vec.push_back(MyClass(10)); vec.push_back(MyClass(20)); vec.push_back(MyClass(40)); // Sorting the vector of custom objects sort(vec.begin(), vec.end()); // Printing the vector cout << "Sorted vector: "; for (auto& obj : vec) cout << obj.data << " "; cout << endl; return 0; } OutputSorted vector: 10 20 30 40 Time Complexity: O(N*logN), where N is the size of the vector.Auxiliary Space: O(logN) Comment More infoAdvertise with us Next Article How to Sort a Vector of Custom Objects in C++? prateekpranveer321 Follow Improve Article Tags : C++ Programs C++ cpp-vector CPP Examples Practice Tags : CPP Similar Reads How to Sort a Vector in a Map in C++? In C++, we can create a map container where the values associated with keys is a vector. In this article, we will learn how to sort a vector within a map in C++. Example Input: myMap = { {3, {9, 7, 3}}, {5, {4, 2, 8, 1, 6}}, {8, {1, 2, 5, 8}} }; Output: Map: Key: 3, Sorted Vector: [3 7 9 ] Key: 5, S 2 min read How to Create std::vector of Custom Classes or Structs in C++? In C++, std::vectors are dynamic arrays that can resize themselves during the runtime, and classes or structs allow the users to create custom data types of their choice. There might be many situations when we want to store a custom data type into a vector. In this article, we will learn how to crea 3 min read How to Sort Vector in Ascending Order in C++? Sort a vector in ascending order means arranging the elements of vector in such a way that the first element will be smallest, second element will be second smallest and so on. In this article, we will learn different ways to sort the vector in ascending order in C++.The most efficient way to sort t 3 min read How to Sort a Vector Using a Custom Comparator in C++? In C++, the std::sort() function sorts the given vector in increasing order by default. The custom comparator is a function that defines the order in which the elements of a std::vector should be sorted. It is passed as the parameter to the std::sort() function.In this article, we will learn how to 4 min read How to Create a Vector of Tuples in C++? In C++, a tuple is an object that allows the users to store elements of various data types together while a vector is used to store elements of the same data types. In this article, we will learn how we can create a vector of tuples in C++. Example: Input: Tuple1 ={10,A,5.3} Tuple2= {20,B,6.5}Output 2 min read How to Find the Median of Sorted Vector in C++? In C++, vectors are dynamic arrays and the median is a middle value when data is sorted in ascending order. In this article, we will learn how to find the median of all elements in a sorted vector in C++. Example Input: myVector = {10, 20, 30, 40, 50}; Output: Median is 30Finding Median of Sorted Ve 2 min read How to Insert an Element in a Sorted Vector in C++? In C++, inserting element in a sorted vector should be done such that it preserves the order of elements. In this article, we will learn different methods to insert an element in a sorted vector in C++.The recommended way to insert an element is to first find the position of insertion using lower_bo 4 min read How to Create a Vector of Arrays in C++? In C++, an array is a collection of elements of a single type while vectors are dynamic arrays as they can change their size during the insertion and deletion of elements. In this article, we will learn how to create a vector of arrays in C++. Example: Input: arr1 = {1, 2, 3}; arr2 = {4, 5, 6}; arr3 2 min read How to Remove Duplicates from a Vector in C++? In this article, we will learn how to remove the duplicates from the vector in C++.The recommended way to remove the duplicates from the vector is by using sort() with unique() to move all the duplicate elements at the end and then remove them using vector erase(). Letâs take a look at an example:C+ 3 min read How to Merge Two Sorted Vector in C++? Merging two sorted vectors means storing the elements of a vector into a single vector in a sorted order. In this article, we will learn how to merge two sorted vectors in C++.The simplest way to merge the two sorted vectors is by using the merge() function. Letâs take a look at an example:C++#inclu 2 min read Like