How to sort an array in C++?
Tags: cpp, oop, programming
Published: 2020-12-17 18:28:04

In C++, we have a function for sorting a vector or array provided by STL. We can sort array in different orders like ascending, descending or in any particular order. And this article we are going to discuss all the methods, approach and the program to sort an array.

---
title: How to sort an array in C++? 
tags: cpp
canonical_url: https://kodlogs.com/blog/644/how-to-sort-an-array-in-c
---

#In ascending order

##Approach
* First we have to define an array.
* Then we’ll get the number of elements by dividing the whole array by the array of one element.
* Then we will use sort() for sorting arrays. We don’t have to worry about sorting in ascending order since the function sort() sorts in ascending order by default.
* And finally the output.

```
// C++ program to demonstrate default behaviour of 
// sort() in STL. 
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    int arr[] = {1, 5, 8, 9, 6, 7, 3, 4, 2, 0}; 
    int n = sizeof(arr)/sizeof(arr[0]); 
  
    sort(arr, arr+n); 
  
    cout << "\nArray after sorting using "
         "default sort is : \n"; 
    for (int i = 0; i < n; ++i) 
        cout << arr[i] << " "; 
  
    return 0; 
} 
``` 

**Output**
Array after sorting using default sort is : 
0 1 2 3 4 5 6 7 8 9 
#In descending order
For sorting in descending order we have to define a third parameter in the function sort() and the parameter is greater(). This function puts the greater element before the one by comparing them.

##Approach
The approach is the same as the previous one.

``
// C++ program to demonstrate descending order sort using 
// greater<>(). 
#include <bits/stdc++.h> 
using namespace std; 
  
int main() 
{ 
    int arr[] = {1, 5, 8, 9, 6, 7, 3, 4, 2, 0}; 
    int n = sizeof(arr)/sizeof(arr[0]); 
  
    sort(arr, arr+n, greater<int>()); 
  
    cout << "Array after sorting : \n"; 
    for (int i = 0; i < n; ++i) 
        cout << arr[i] << " "; 
  
    return 0; 
} 
``

**Output**
Array after sorting : 
9 8 7 6 5 4 3 2 1 0 

#In particular order
For sorting an array in particular order, we can also write our own comparator function and we can pass it as a third parameter. The third parameter which is our own comparator returns a value, which is convertible to bool; which tells us whether the passed first argument should be placed before the second passed argument or not.

##Approach
Same as the previous one.
```
// A C++ program to demonstrate STL sort() using 
// our own comparator 
#include<bits/stdc++.h> 
using namespace std; 
  
// An interval has a start time and end time 
struct Interval 
{ 
    int start, end; 
}; 
  
// Compares two intervals according to staring times. 
bool compareInterval(Interval i1, Interval i2) 
{ 
    return (i1.start < i2.start); 
} 
  
int main() 
{ 
    Interval arr[] =  { {6,8}, {1,9}, {2,4}, {4,7} }; 
    int n = sizeof(arr)/sizeof(arr[0]); 
  
    // sort the intervals in increasing order of 
    // start time 
    sort(arr, arr+n, compareInterval); 
  
    cout << "Intervals sorted by start time : \n"; 
    for (int i=0; i<n; i++) 
       cout << "[" << arr[i].start << "," << arr[i].end 
            << "] "; 
  
    return 0; 
} 
```

**Output** 
Intervals sorted by start time : 
[1,9] [2,4] [4,7] [6,8]