Raku By Example
View me onGitHub
## sort by key

my %hash =
    two   => 2,
    three => 3,
    one   => 1,
;

for %hash.sort(*.key)>>.kv -> ($key, $value) {
    say "'$key' => '$value'";
}

## sort and destructuring

my %hash =
    two   => 2,
    three => 3,
    one   => 1,
;

for %hash.sort -> (:$key, :$value) {
  say "'$key' => '$value'";
}

say "-" x 15;

for %hash.sort -> $pair (:$key, :$value) {
  say $pair;
  say $key === $pair.key and $value === $pair.value; # True
}

say "-" x 15;

# :$key is short for :key($key)
for %hash.sort -> (:key($k), :value($v)) {
  say "'$k' => '$v'";
}

## sort hash by value descending

my %hash = 'Perl'=>80,
         'Python'=>100,
             'Go'=>95,
            'CMD'=>20,
            "Php"=>80,
           "Java"=>85;

for %hash.sort({-.value}).hash.keys -> $key {
    say $key, "\t", %hash{"$key"}
}

## 成绩排序

#`(
-- example.txt
eline	A-
Zsófia	B+
João	F
Maryam	B+
秀英	B-
Finn	D+
Aarav	A
Emma	F
Omar	B
)

my %grade = "example.txt".IO.lines.map: {
    m:s/^(\w+) (<[A..F]><[+-]>?)$/
        or die "Can't parse line '$_'";

    ~$0 => ~$1;
}

say %grade;

say "Zsófia's grade: %grade<Zsófia>";
say "List of students with a failing grade:";

say " " ~%grade.grep(*.value ge 'F')».key.join(" ");

say "Distribution of grades by letter:";
say "  {.key}: {+.value} student{"s" if .value != 1}"
    for %grade.classify(*.value.comb[0]).sort(*.key);

## sort by object attaribute

# Sort an array of composite structures by a key. For example, if you define a composite structure that presents a name-value pair (in pseudocode): 
# Define structure pair such that:
   # name as a string
   # value as a string


# and an array of such pairs:
# x: array of pairs


# then define a sort routine that sorts the array x by the key name.
# This task can always be accomplished with Sorting Using a Custom Comparator. 

my class Employee {
   has Str $.name;
   has Rat $.wage;
}

my $boss     = Employee.new( name => "Frank Myers"     , wage => 6755.85 );
my $driver   = Employee.new( name => "Aaron Fast"      , wage => 2530.40 );
my $worker   = Employee.new( name => "John Dude"       , wage => 2200.00 );
my $salesman = Employee.new( name => "Frank Mileeater" , wage => 4590.12 );

my @team = $boss, $driver, $worker, $salesman;

my @orderedByName = @team.sort( *.name )».name;
my @orderedByWage = @team.sort( *.wage )».name;

say "Team ordered by name (ascending order):";
say @orderedByName.join("  ");
say "Team ordered by wage (ascending order):";
say @orderedByWage.join("  ");

# this produces the following output:
# Team ordered by name (ascending order):
# Aaron Fast   Frank Mileeater   Frank Myers   John Dude
# Team ordered by wage (ascending order):
# John Dude   Aaron Fast   Frank Mileeater   Frank Myers


# Note that when the sort receives a unary function, it automatically generates an appropriate comparison function based on the type of the data.

say "--" x 23;
say @team.sort({.wage} )».name;

.name.say for @team.sort: {.wage} # ».name;

## sort object

class Student {
    has Str $.name;
    has Int $.grade is rw;
}

my $stu1 = Student.new(name => "zhangwuji", grade => 124);
my $stu2 = Student.new(name => "yangguo",   grade => 128);     
my $stu3 = Student.new(name => "zhaomin",   grade => 145);
my $stu4 = Student.new(name => "sunyizhe",  grade => 145);
my $stu5 = Student.new(name => "zhouziruo", grade => 128);
my $stu6 = Student.new(name => "qiaofeng",  grade => 124);

my @students = $stu1, $stu2,$stu3,$stu4,$stu5,$stu6;

# classify 
# classify 返回一个散列
for @students.classify( *.grade ).sort -> $group {
    say "These students got grade $group.key():";
    say .name for $group.value.list;
}

# sort
my @c = @students.sort: -> $a, $b {$a.grade <=> $b.grade};
say @c.perl;

## sort by nature

use Sort::Naturally;

my @a = $=finish.lines;
.say for @a.sort: { .&naturally };

=finish
1.brief-introduction.adoc
10.javascript-to-perl6-nutshell.adoc
11.python-to-perl6-nutshell.adoc
12.ruby-to-perl6.adoc
13.classes-and-objects.adoc