From 37d13f5c76c35d03a69b6ff27d9f29b7699bbef0 Mon Sep 17 00:00:00 2001
From: quester <quester.pm@gmail.com>
Date: Tue, 13 Apr 2010 00:03:45 -1000
Subject: [PATCH] Fix Parcel.sort (fixes the very first example in http://cloud.github.com/downloads/perl6/book/book-2010-04.pdf) and Hash.sort

---
 src/core/Hash.pm   |    9 +++++++++
 src/core/Parcel.pm |    2 ++
 src/core/Seq.pm    |   10 ++++++++--
 3 files changed, 19 insertions(+), 2 deletions(-)

diff --git a/src/core/Hash.pm b/src/core/Hash.pm
index 29f6f4c..31ca6aa 100644
--- a/src/core/Hash.pm
+++ b/src/core/Hash.pm
@@ -101,4 +101,13 @@ role Hash is EnumMap {
             self.{$key} = $value;
         }
     }
+
+    multi method sort(&by = &infix:<cmp>) {
+        self.pairs.sort(&by)
+    }
+
 }
+
+multi sub sort (%h, :&by = &infix:<cmp>) { %h.pairs.sort(&by) }
+multi sub sort (&by, %h) { %h.pairs.sort(&by) }
+
diff --git a/src/core/Parcel.pm b/src/core/Parcel.pm
index 22014b0..3f060ac 100644
--- a/src/core/Parcel.pm
+++ b/src/core/Parcel.pm
@@ -5,6 +5,8 @@ augment class Parcel {
 
     method rotate(Int $n = 1) { self.Seq.rotate($n) }
 
+    multi method sort(&by = &infix:<cmp>) { self.Seq.sort(&by) }
+
     multi method ACCEPTS($x) {
         # smart-matching against Nil
         if self.elems == 0 {
diff --git a/src/core/Seq.pm b/src/core/Seq.pm
index 3b14404..cac25c2 100644
--- a/src/core/Seq.pm
+++ b/src/core/Seq.pm
@@ -61,17 +61,20 @@ augment class Seq {
         # of indices (from 0 to $list.elems), then use that RPA
         # as a slice into self.
 
+        my $index_PARROT_RPA = pir::new__PS("ResizablePMCArray");
+        pir::push__vPP($index_PARROT_RPA, $_) for ^self.elems; 
+
         # If &by.arity < 2, then it represents a block to be applied
         # to the elements to obtain the values for sorting.
         if (&by.?arity // 2) < 2 {
             my $list = self.map(&by).eager;
-            self[(^pir::elements($list)).eager.sort(
+            self[$index_PARROT_RPA.sort(
                 -> $a, $b { $list[$a] cmp $list[$b] || $a <=> $b }
             )];
         }
         else {
             my $list = self.eager;
-            self[(^pir::elements($list)).eager.sort(
+            self[$index_PARROT_RPA.sort(
                 -> $a, $b { &by($list[$a],$list[$b]) || $a <=> $b }
             )];
         }
@@ -87,4 +90,7 @@ augment class Seq {
     }
 }
 
+multi sub sort (@x, :&by = &infix:<cmp>) { @x.sort(&by) }
+multi sub sort (&by, @x) { @x.sort(&by) }
+
 # vim: ft=perl6
-- 
1.6.6.1

