[% setvar title Sort order for any hash %]

This file is part of the Perl 6 Archive

Note: these documents may be out of date. Do not use as reference!

To see what is currently happening visit http://www.perl6.org/


Sort order for any hash


  Maintainer: David Nicol <perl6rfc@davidnicol.com>
  Date: 17 Aug 2000
  Mailing List: perl6-language@perl.org
  Number: 124
  Version: 1
  Status: Developing


Herein a new syntax is introduced to specify a sort function for the keys of any hash.


	%professors{ $a->name cmp $b->name };

	%students{ $$students{$b}{GPA} <=> $$students{$a}{GPA} };

is proposed as an extension to specify a "sort order" on the values returned from the each, keys, and values functions regarding the associative array.

When the assignment is made, the expression returns the an anonymous coderef which can be saved and restored to the hash later if needed.

The expression can also be made temporary by use of the my or local operators.

The side effects of making a large, tied array a sorted large tied array are possibly dangerous.


A "sort function" will need to be added into the list of magics maintained for every associative array.

A set of macros in terms of sort will need to be defined which replace keys, values, and each functions on sorted arrays

Since the proposed syntax is currently an error, little modification to the parser will be required: Fatal syntax errors may be filtered for this instance.