[alg.three.way]

# 26 Algorithms library [[algorithms]](./#algorithms)

## 26.8 Sorting and related operations [[alg.sorting]](alg.sorting#alg.three.way)

### 26.8.12 Three-way comparison algorithms [alg.three.way]

[ð](#lib:lexicographical_compare_three_way)

`template<class InputIterator1, class InputIterator2, class Cmp>
  constexpr auto
    lexicographical_compare_three_way(InputIterator1 b1, InputIterator1 e1,
                                      InputIterator2 b2, InputIterator2 e2,
                                      Cmp comp)
      -> decltype(comp(*b1, *b2));
`

[1](#1)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L11857)

Let N be min(e1 - b1, e2 - b2)[.](#1.sentence-1)

Let E(n) be comp(*(b1 + n), *(b2 + n))[.](#1.sentence-2)

[2](#2)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L11861)

*Mandates*: decltype(comp(*b1, *b2)) is a comparison category type[.](#2.sentence-1)

[3](#3)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L11865)

*Returns*: E(i), where i is the smallest integer in [0, N)
such that E(i) != 0 is true, or(e1 - b1) <=> (e2 - b2) if no such integer exists[.](#3.sentence-1)

[4](#4)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L11871)

*Complexity*: At most N applications of comp[.](#4.sentence-1)

[ð](#lib:lexicographical_compare_three_way_)

`template<class InputIterator1, class InputIterator2>
  constexpr auto
    lexicographical_compare_three_way(InputIterator1 b1, InputIterator1 e1,
                                      InputIterator2 b2, InputIterator2 e2);
`

[5](#5)

[#](http://github.com/Eelis/draft/tree/9adde4bc1c62ec234483e63ea3b70a59724c745a/source/algorithms.tex#L11885)

*Effects*: Equivalent to:return lexicographical_compare_three_way(b1, e1, b2, e2, compare_three_way());
