# This is pseudo code for each $record (@records) { push $record into the right $bin; $records++; } $key = 1; while ($records--) { ++$key while(empty $bin{$key}); pop $record from $bin{$key} and print it; }