# Sorting of Vector of Tuple in C++ (Ascending Order)

What is Vector of Tuple?
A tuple is an object that can hold a number of elements and a vector containing multiple number of such tuple is called a vector of tuple. The elements can be of different data types. The elements of tuples are initialized as arguments in order in which they will be accessed.

 `// C++ program to demonstrate vector of tuple ` `#include ` `using` `namespace` `std; ` `int` `main() ` `{ ` `    ``vector > v; ` `    ``v.push_back(make_tuple(10, 20, 30)); ` `    ``v.push_back(make_tuple(15, 5, 25)); ` `    ``v.push_back(make_tuple(3, 2, 1)); ` ` `  `    ``// Printing vector tuples ` `    ``for` `(``int` `i = 0; i < v.size(); i++)  ` `        ``cout << get<0>(v[i]) << ``" "`  `             ``<< get<1>(v[i]) << ``" "`  `             ``<< get<2>(v[i]) << ``"\n"``; ` `     `  `    ``return` `0; ` `} `

Output:

```10 20 30
15 5 25
3 2 1
```

Different ways to sort vector of tuples
Case 1 : Sorting the vector elements on the basis of first element of tuples in ascending order.
This type of sorting can be achieved using simple “ sort() ” function. By default the sort function sorts the vector elements on basis of first element of tuples.

 `// C++ program to demonstrate sorting in ` `// vector of tuple according to 1st element ` `// of tuple ` `#include ` `using` `namespace` `std; ` `int` `main() ` `{ ` `    ``vector > v; ` `    ``v.push_back(make_tuple(10, 20, 30)); ` `    ``v.push_back(make_tuple(15, 5, 25)); ` `    ``v.push_back(make_tuple(3, 2, 1)); ` ` `  `    ``// Using sort() function to sort by 1st  ` `    ``// element of tuple ` `    ``sort(v.begin(), v.end()); ` `    ``cout << ``"Sorted Vector of Tuple on basis"` `           ``" of first element of tuple:\n"``; ` `    ``for` `(``int` `i = 0; i < v.size(); i++)  ` `        ``cout << get<0>(v[i]) << ``" "`  `             ``<< get<1>(v[i]) << ``" "` `             ``<< get<2>(v[i]) << ``"\n"``; ` `     `  `    ``return` `0; ` `} `

Output:

```Sorted Vector of Tuple on basis of first element of tuple:
3 2 1
10 20 30
15 5 25
```

Case 2 : Sorting the vector elements on the basis of second element of tuples in ascending order.
There are instances when we require to sort the elements of vector on the basis of second elements of tuples. For that, we modify the sort() function and we pass a third argument, a call to an user defined explicit function in the sort() function.

 `// C++ program to demonstrate sorting in vector ` `// of tuple according to 2nd element of tuples ` `#include ` `using` `namespace` `std; ` ` `  `// Comparison function to sort the vector elements ` `// by second element of tuples ` `bool` `sortbysec(``const` `tuple<``int``, ``int``, ``int``>& a,  ` `               ``const` `tuple<``int``, ``int``, ``int``>& b) ` `{ ` `    ``return` `(get<1>(a) < get<1>(b)); ` `} ` ` `  `int` `main() ` `{ ` `    ``vector > v; ` `    ``v.push_back(make_tuple(10, 20, 30)); ` `    ``v.push_back(make_tuple(15, 5, 25)); ` `    ``v.push_back(make_tuple(3, 2, 1)); ` ` `  `    ``// Using sort() function to sort by 2nd element ` `    ``// of tuple ` `    ``sort(v.begin(), v.end(), sortbysec); ` `    ``cout << ``"Sorted Vector of Tuple on basis"` `           ``" of Second element of tuple:\n"``; ` ` `  `    ``for` `(``int` `i = 0; i < v.size(); i++)  ` `        ``cout << get<0>(v[i]) << ``" "`  `             ``<< get<1>(v[i]) << ``" "`  `             ``<< get<2>(v[i]) << ``"\n"``; ` `    ``return` `0; ` `} `

Output:

```Sorted Vector of Tuple on basis of Second element of tuple:
3 2 1
15 5 25
10 20 30
```

Case 3 : Sorting the vector elements on the basis of third element of tuples in ascending order.
There are instances when we require to sort the elements of vector on the basis of third elements of tuples. For that, we modify the sort() function and we pass a third argument, a call to an user defined explicit function in the sort() function.

 `// C++ program to demonstrate sorting in vector ` `// of tuple according to 3rd element of tuple ` `#include ` `using` `namespace` `std; ` `// Driver function to sort the vector elements ` `// by third element of tuple ` `bool` `sortbyth(``const` `tuple<``int``, ``int``, ``int``>& a,  ` `              ``const` `tuple<``int``, ``int``, ``int``>& b) ` `{ ` `    ``return` `(get<2>(a) < get<2>(b)); ` `} ` ` `  `int` `main() ` `{ ` `    ``vector > v; ` `    ``v.push_back(make_tuple(10, 20, 30)); ` `    ``v.push_back(make_tuple(15, 5, 25)); ` `    ``v.push_back(make_tuple(3, 2, 1)); ` ` `  `    ``// Using sort() function to sort by 3rd element ` `    ``// of tuple ` `    ``sort(v.begin(), v.end(), sortbyth); ` `    ``cout << ``"Sorted Vector of Tuple on basis"` `            ``" of Third element of tuple:\n"``; ` `    ``for` `(``int` `i = 0; i < v.size(); i++)  ` `        ``cout << get<0>(v[i]) << ``" "`  `             ``<< get<1>(v[i]) << ``" "`  `             ``<< get<2>(v[i]) << ``"\n"``; ` `     `  `    ``return` `0; ` `} `

Output:

```Sorted Vector of Tuple on basis of Third element of tuple:
3 2 1
15 5 25
10 20 30
```

GeeksforGeeks has prepared a complete interview preparation course with premium videos, theory, practice problems, TA support and many more features. Please refer Placement 100 for details

My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Improved By : suri_kumkaran

Article Tags :
Practice Tags :

8

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.