txt: ## cmp.md
  cmp
  
  Combined from primary sources listed below.
  
  # [In List](#___top "go to top of document")[§](#(List)_infix_cmp "direct link")
  
  See primary documentation
  [in context](/type/List#infix_cmp)
  for **infix cmp**.
  
  ```raku
  multi infix:<cmp>(List @a, List @b)
  ```
  
  Evaluates `Lists` by comparing element `@a[$i]` with `@b[$i]` (for some `Int $i`, beginning at 0) and returning `Order::Less`, `Order::Same`, or `Order::More` depending on if and how the values differ. If the operation evaluates to `Order::Same`, `@a[$i + 1]` is compared with `@b[$i + 1]`. This is repeated until one is greater than the other or all elements are exhausted.
  
  If the `List`s are of different lengths, at most only `$n` comparisons will be made (where `$n = @a.elems min @b.elems`). If all of those comparisons evaluate to `Order::Same`, the final value is selected based upon which `List` is longer.
  
  ```raku
  say (1, 2, 3) cmp (1, 2, 3);   # OUTPUT: «Same␤»
  say (4, 5, 6) cmp (4, 5, 7);   # OUTPUT: «Less␤»
  say (7, 8, 9) cmp (7, 8, 8);   # OUTPUT: «More␤»
  
  say (1, 2)    cmp (1, 2, 3);   # OUTPUT: «Less␤»
  say (1, 2, 3) cmp (1, 2);      # OUTPUT: «More␤»
  say (9).List  cmp (^10).List;  # OUTPUT: «More␤»
  ```
  
  # [In Pair](#___top "go to top of document")[§](#(Pair)_infix_cmp "direct link")
  
  See primary documentation
  [in context](/type/Pair#infix_cmp)
  for **infix cmp**.
  
  ```raku
  multi infix:<cmp>(Pair:D, Pair:D)
  ```
  
  The type-agnostic comparator; compares two `Pair`s. Compares first their *key* parts, and then compares the *value* parts if the keys are equal.
  
  ```raku
  my $a = (Apple => 1);
  my $b = (Apple => 2);
  say $a cmp $b; # OUTPUT: «Less␤»
  ```
  
  # [In Operators](#___top "go to top of document")[§](#(Operators)_infix_cmp "direct link")
  
  See primary documentation
  [in context](/language/operators#infix_cmp)
  for **infix cmp**.
  
  ```raku
  multi infix:<cmp>(Any,       Any)
  multi infix:<cmp>(Real:D,    Real:D)
  multi infix:<cmp>(Str:D,     Str:D)
  multi infix:<cmp>(Version:D, Version:D)
  ```
  
  Generic, "smart" three-way comparator.
  
  Compares strings with string semantics, numbers with number semantics, [`Pair`](/type/Pair) objects first by key and then by value etc.
  
  if `$a eqv $b`, then `$a cmp $b` always returns `Order::Same`.
  
  ```raku
  say (a => 3) cmp (a => 4);   # OUTPUT: «Less␤»
  say 4        cmp 4.0;        # OUTPUT: «Same␤»
  say 'b'      cmp 'a';        # OUTPUT: «More␤»
  ```
  
  Strings are compared codepoint by codepoint; if leading codepoints are the same, the result of comparing the first differing codepoint is returned or the longer string if their lengths differ.
  
  ```raku
  "abcd" cmp "abcde";    # OUTPUT: «Less␤»
  "abcd " cmp "abcde";   # OUTPUT: «Less␤»
  'A' cmp 'Ẳ';           # OUTPUT: «Less␤»
  ```
  
  # [In enum Order](#___top "go to top of document")[§](#(enum_Order)_infix_cmp "direct link")
  
  See primary documentation
  [in context](/type/Order#infix_cmp)
  for **infix cmp**.
  
  ```raku
  multi infix:<cmp>(\a, \b --> Order:D)
  ```
  
  `cmp` will first try to compare operands as strings (via coercion to [`Stringy`](/type/Stringy)), and, failing that, will try to compare numerically via the `<=>` operator or any other type-appropriate comparison operator. See also [the documentation for the `cmp` operator](/routine/cmp#(Operators)_infix_cmp).
  
  # [In Allomorph](#___top "go to top of document")[§](#(Allomorph)_infix_cmp "direct link")
  
  See primary documentation
  [in context](/type/Allomorph#infix_cmp)
  for **infix cmp**.
  
  ```raku
  multi infix:<cmp>(Allomorph:D $a, Allomorph:D $b)
  ```
  
  Compare two `Allomorph` objects. The comparison is done on the [`Numeric`](/type/Numeric) value first and then on the [`Str`](/type/Str) value. If you want to compare in a different order then you would coerce to a [`Numeric`](/type/Numeric) or [`Str`](/type/Str) value first:
  
  ```raku
  my $f = IntStr.new(42, "smaller");
  my $g = IntStr.new(43, "larger");
  say $f cmp $g;          # OUTPUT: «Less␤»
  say $f.Str cmp $g.Str;  # OUTPUT: «More␤»
  ```
  