Documentation for routine cmp

Documentation for routine cmp, assembled from the following types:

language documentation Operators

From Operators

(Operators) infix cmp

multi sub infix:<cmp>(Any,       Any)
multi sub infix:<cmp>(Real:D,    Real:D)
multi sub infix:<cmp>(Str:D,     Str:D)
multi sub infix:<cmp>(Version:DVersion:D)

Generic, "smart" three-way comparator.

Compares strings with string semantics, numbers with number semantics, Pair objects first by key and then by value etc.

if $a eqv $b, then $a cmp $b always returns Order::Same.

say (=> 3cmp (=> 4);   # OUTPUT: «Less␤» 
say 4        cmp 4.0;        # OUTPUT: «Same␤» 
say 'b'      cmp 'a';        # OUTPUT: «More␤» 

class ComplexStr

From ComplexStr

(ComplexStr) infix cmp

multi sub infix:<cmp>(ComplexStr:D $aComplexStr:D $b)

Compare two ComplexStr objects. The comparison is done on the Complex value first and then on the Str value. If you want to compare in a different order then you would coerce to the Complex or Str values first:

my $f = ComplexStr.new(42+0i, "smaller");
my $g = ComplexStr.new(43+0i, "larger");
say $f cmp $g;          # OUTPUT: «Less␤» 
say $f.Str cmp $g.Str;  # OUTPUT: «More␤» 

class IntStr

From IntStr

(IntStr) infix cmp

multi sub infix:<cmp>(IntStr:D $aIntStr:D $b)

Compare two IntStr objects. The comparison is done on the Int value first and then on the Str value. If you want to compare in a different order then you would coerce to a Int or Str value first:

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␤» 

class NumStr

From NumStr

(NumStr) infix cmp

multi sub infix:<cmp>(NumStr:D $aNumStr:D $b)

Compare two NumStr objects. The comparison is done on the Num value first and then on the Str value. If you want to compare in a different order then you would coerce to a Num or Str value first:

my $f = NumStr.new(42.1e0"smaller");
my $g = NumStr.new(43.1e0"larger");
say $f cmp $g;          # OUTPUT: «Less␤» 
say $f.Str cmp $g.Str;  # OUTPUT: «More␤» 

enum Order

From Order

(Order) infix cmp

multi sub infix:<cmp>(\a, \b --> Order:D)

Coerces its arguments to Stringy.

class Pair

From Pair

(Pair) infix cmp

Defined as:

multi sub infix:<cmp>(Pair:DPair:D)

The type-agnostic comparator; compares two Pairs. Compares first their key parts, and then compares the value parts if the keys are equal.

my $a = (Apple => 1);
my $b = (Apple => 2);
say $a cmp $b;

class RatStr

From RatStr

(RatStr) infix cmp

multi sub infix:<cmp>(RatStr:D $aRatStr:D $b)

Compare two RatStr objects. The comparison is done on the Rat value first and then on the Str value. If you want to compare in a different order then you would coerce to the Rat or Str values first:

my $f = RatStr.new(42.1"smaller");
my $g = RatStr.new(43.1"larger");
say $f cmp $g;          # OUTPUT: «Less␤» 
say $f.Str cmp $g.Str;  # OUTPUT: «More␤»