How to Sort a List in C++ STL? Last Updated : 21 Apr, 2024 Comments Improve Suggest changes Like Article Like Report In C++, a list is a sequence container provided by the STL library of C++ that provides the features of a doubly linked list and stores the data in non-contiguous memory locations efficiently. In this article, we will learn how to sort a list in C++. Example: Input: myList = {30, 10, 20, 40, 50};Output: // Sorted listmyList = {10, 20, 30, 40, 50};Sort a List in C++To sort a std::list in C++, we can use the std::list::sort function. This function sorts the elements stored in a list by changing their position within the list only as a result the original list is modified but the order of equal elements remains preserved. Syntax to Sort a List in C++list_Name.sort();Here, list_Name is the name of the list container and no parameters are passed. C++ Program to Sort a List The below program demonstrates how we can use the list::sort() to sort a list in C++. C++ // C++ program to sort a list #include <iostream> #include <list> using namespace std; int main() { // Initializing a list of integers list<int> myList = { 30, 10, 20, 40, 50 }; // Printing the list before sorting cout << "List before sorting : "; for (int num : myList) { cout << num << " "; } cout << endl; // Sorting the list myList.sort(); // Printing the list after sorting cout << "List after sorting : "; for (int num : myList) { cout << num << " "; } return 0; } OutputList before sorting : 30 10 20 40 50 List after sorting : 10 20 30 40 50 Time Complexity: O(N logN), here N is the size of the list.Auxiliary Space: O(1) Comment More infoAdvertise with us Next Article How to Sort a List in C++ STL? P pantharshx9d9 Follow Improve Article Tags : C++ Programs C++ STL cpp-list cpp-list-functions CPP Examples +2 More Practice Tags : CPPSTL Similar Reads How to Reverse a List in C++ STL? In C++, std::list is a sequence container that allows non-contiguous memory allocation. As such, it is a doubly linked list that can be traversed in both directions. In this article, we will learn how to reverse a list in C++. Example: Input: myList = {10, 20, 30, 40, 50}; Output: Reversed List: 50 2 min read How to Sort a List of Pairs in C++? In C++, the pair container allows the users to store two different types of objects as a single unit. We can store the pairs in a list if we want to store multiple pairs in a single place. Lists are sequence containers that allow non-contiguous memory allocation. In this article, we will learn how t 4 min read How to Copy a List in C++ STL? In C++, a list is a sequence container provided by the STL library that represents a doubly linked list and allows us to store data in non-contiguous memory locations efficiently. In this article, we will learn how to copy one list to another in C++. Input: sourceList = {10, 20, 30, 40, 50};Output: 2 min read How to Find the Size of a List in C++? In C++, Standard Template Library (STL) we have a std::list container that is a doubly-linked list in which elements are stored in non-contiguous memory allocation. In this article, we will learn how to find the size of a list in C++ STL. Example: Input: myList = {10, 20, 30, 40, 50}; Output: Size o 2 min read How to Create a Stack of Lists in C++? In C++, a list is a sequence container that allows dynamic insertion and deletion operations, whereas a stack is a data structure that follows last-in, first-out (LIFO). In this article, we will learn how to create a stack of lists in C++. Example: Input: list1 = { 1, 2, 3, 4 }list2 = { 5, 6, 7 }Out 2 min read How to Compare Two Lists in C++ STL? In C++, lists are containers provided by the STL library of C++, which allows us to store elements of the same data type in non-contiguous memory locations. Comparing two lists is a very common operation while using lists. In this article, we will learn how to compare two lists in C++. Example: Inpu 2 min read 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 Sort a Deque in C++? In C++, the STL provides a container called a double-ended queue which is popularly known as deque. This container allows fast insertions and deletions at both ends of the container. In this article, we will learn how to sort a deque in C++. Example: Input: myDeque = {30, 10, 20,50,40} Output: 10 20 2 min read How to Sort an Array in C++? Sorting an array involves rearranging its elements in a specific order such as from smallest to largest element or from largest to smallest element, etc. In this article, we will learn how to sort an array in C++.Example:Input: arr ={5,4,1,2,3}Output: 1 2 3 4 5Explanation: arr is sorted in increasin 4 min read How to Declare a List in C++? In C++, list is a data structure used to store elements sequentially in non-contiguous memory locations. This container implements doubly linked list which contains pointers to both previous and next elements in the sequence. In this article, we will learn how to declare a list in C++. Declare a Lis 2 min read Like