Skip to content

vector_pro::sort

C++
void sort() noexcept;

void sort(int(compare2)(const value_type &, const value_type &)) noexcept;

Sort the vector

Sorts the vector by Timsort.

To note that you must ensure your value_type implements the > and the < comparison operator, or you must pass a function of the type int(const value_type &, const value_type &) as a parameter.

Parameters

  1. int(compare2)(const value_type &, const value_type &)
    To let us understand how to compare two value_type.

Return value

none

Example

C++
// vector_pro::sort
#include <iostream>
#include "vector_pro.h"

/**
 * Output:
 * myvector is now:: [ 32, 72, 55, 46, 32, 12, 61, 84, 39, 97 ]
 * myvector after sorting:: [ 12, 32, 32, 39, 46, 55, 61, 72, 84, 97 ]
 */

int main ()
{

  // initialize the container with 10 elements
  vector_pro<int> myvector = { 32, 72, 55, 46, 32, 12, 61, 84, 39, 97 };
  std::cout << "myvector is now:: " << myvector << std::endl;

  // sort
  myvector.sort();
  std::cout << "myvector after sorting:: " << myvector << std::endl;

  return 0;
}

Complexity

Average performance will be \(O(nlogn)\).

See also 1 and 2 .