SPVM::Sort(3)

πŸ“¦ version 0.990125, in alpine 3.24

πŸ¦‹ SPVM::Sort(3) β€” Sorting Functions πŸ¦‹

bestie, welcome to SPVM::Sort(3), aka the SPVM sorting-functions page. It's giving stable merge sort, in-place glow-ups, and a whole lot of comparator energy. I'm obsessed. πŸ’…

✨ Name ✨

SPVM::Sort β€” Sorting Functions. That's the whole vibe.

🌸 Usage 🌸

okay so here's the full demo dump, exactly as written β€” every example, every comment, every little crumb. πŸ“

# Sort a byte array in-place by asc order
my $array = [(byte)2, 3, 1];
Sort->sort_byte($array, method : int ($a : int, $b : int) {
  return $a <=> $b;
});

# Sort short array in-place by asc order
my $array = [(short)2, 3, 1];
Sort->sort_short($array, method : int ($a : int, $b : int) {
  return $a <=> $b;
});

# Sort int array in-place by asc order
my $array = [2, 3, 1];
Sort->sort_int($array, method : int ($a : int, $b : int) {
  return $a <=> $b;
});

# Sort long array in-place by asc order
my $array = [(long)2, 3, 1];
Sort->sort_long($array, method : int ($a : long, $b : long) {
  return $a <=> $b;
});

# Sort float array in-place by asc order
my $array = [(float)2, 3, 1];
Sort->sort_float($array, method : int ($a : float, $b : float) {
  return $a <=> $b;
});

# Sort double array in-place by asc order
my $array = [(double)2, 3, 1];
Sort->sort_double($array, method : int ($a : double, $b : double) {
  return $a <=> $b;
});

# Sort string array in-place by asc order
my $array = ["11", "1", "2", undef, ""];
Sort->sort_string($array, method : int ($a : string, $b : string) {
  return $a cmp $b;
});

# Sort object array in-place by asc order
my $minimals = new TestCase::Minimal[3];
$minimals->[0] = TestCase::Minimal->new;
$minimals->[0]{x} = 3;
$minimals->[0]{y} = 5;
$minimals->[1] = TestCase::Minimal->new;
$minimals->[1]{x} = 3;
$minimals->[1]{y} = 7;
$minimals->[2] = TestCase::Minimal->new;
$minimals->[2]{x} = 2;
$minimals->[2]{y} = 9;
Sort->sort_object($minimals, method : int ($object1 : object, $object2 : object) {
  my $minimal1 = (TestCase::Minimal)$object1;
  my $minimal2 = (TestCase::Minimal)$object2;

  return $minimal1->{x} <=> $minimal2->{x} || $minimal1->{y} <=> $minimal2->{y};
});

πŸ’– Description πŸ’–

Sort provides sorting functions, and the sorting algorithm is a stable merge sort. Yes bestie, stable β€” equal elements don't get chaotic. Iconic behavior. ✨

πŸ‘‘ Class Methods πŸ‘‘


🌈 sort_byte 🌈

static method sort_byte : void ($array : byte[], $comparator : Comparator::Int, $offset : int = 0, $length : int = -1);

Sorts the range of the elements of byte $array in-place. The sorted range is from $offset to the position proceeded by $length.

If the length is less than 0, the length to the end of the string is calculated from the length of the array and the offset.

The Comparator::Int comparator is used to compare each element.

Exceptions:

  • $array must be defined.
  • $offset must be greater than or equal to 0.
  • $comparator must be defined.
  • $offset + length must be less than or equal to the length of $elements.

πŸŽ€ sort_byte_asc πŸŽ€

method sort_byte_asc : void ($array : byte[], $offset : int = 0, $length : int = -1);

The alias for the following code using sort_byte:

Sort->sort_byte($array, method : int ($a : int, $b : int) { return $a <=> $b; }, $offset, $length);

πŸ¦‹ sort_byte_desc πŸ¦‹

static method sort_byte_desc : void ($array : byte[], $offset : int = 0, $length : int = -1);

The alias for the following code using sort_byte:

Sort->sort_byte($array, method : int ($a : int, $b : int) { return $b <=> $a; }, $offset, $length);

⭐ sort_double ⭐

static method sort_double : void ($array : double[], $comparator : Comparator::Double, $offset : int = 0, $length : int = -1);

Sorts the range of the elements of double $array in-place. The sorted range is from $offset to the position proceeded by $length.

If the length is less than 0, the length to the end of the string is calculated from the length of the array and the offset.

The Comparator::Double comparator is used to compare each element.

Exceptions:

  • $array must be defined.
  • $offset must be greater than or equal to 0.
  • $comparator must be defined.
  • $offset + length must be less than or equal to the length of $elements.

πŸ’œ sort_double_asc πŸ’œ

static method sort_double_asc : void ($array : double[], $offset : int = 0, $length : int = -1);

The alias for the following code using sort_double:

Sort->sort_double($array, method : int ($a : double, $b : double) { return $a <=> $b; }, $offset, $length);

🌸 sort_double_desc 🌸

static method sort_double_desc : void ($array : double[], $offset : int = 0, $length : int = -1);

The alias for the following code using sort_double:

Sort->sort_double($array, method : int ($a : double, $b : double) { return $b <=> $a; }, $offset, $length);

🧚 sort_float 🧚

static method sort_float : void ($array : float[], $comparator : Comparator::Float, $offset : int = 0, $length : int = -1);

Sorts the range of the elements of float $array in-place. The sorted range is from $offset to the position proceeded by $length.

If the length is less than 0, the length to the end of the string is calculated from the length of the array and the offset.

The Comparator::Float comparator is used to compare each element.

Exceptions:

  • $array must be defined.
  • $offset must be greater than or equal to 0.
  • $comparator must be defined.
  • $offset + length must be less than or equal to the length of $elements.

πŸ“ sort_float_asc πŸ“

static method sort_float_asc : void ($array : float[], $offset : int = 0, $length : int = -1);

The alias for the following code using sort_float:

Sort->sort_float($array, method : int ($a : float, $b : float) { return $a <=> $b; }, $offset, $length);

πŸ’€ sort_float_desc πŸ’€

static method sort_float_desc : void ($array : float[], $offset : int = 0, $length : int = -1);

The alias for the following code using sort_float:

Sort->sort_float($array, method : int ($a : float, $b : float) { return $b <=> $a; }, $offset, $length);

🧚 sort_float 🧚

static method sort_float : void ($array : float[], $comparator : Comparator::Float, $offset : int = 0, $length : int = -1);

Sorts the range of the elements of float $array in-place. The sorted range is from $offset to the position proceeded by $length.

If the length is less than 0, the length to the end of the string is calculated from the length of the array and the offset.

The Comparator::Float comparator is used to compare each element.

Exceptions:

  • $array must be defined.
  • $offset must be greater than or equal to 0.
  • $comparator must be defined.
  • $offset + length must be less than or equal to the length of $elements.

πŸ“ sort_float_asc πŸ“

static method sort_float_asc : void ($array : float[], $offset : int = 0, $length : int = -1);

The alias for the following code using sort_float:

Sort->sort_float($array, method : int ($a : float, $b : float) { return $a <=> $b; }, $offset, $length);

πŸ’€ sort_float_desc πŸ’€

static method sort_float_desc : void ($array : float[], $offset : int = 0, $length : int = -1);

The alias for the following code using sort_float:

Sort->sort_float($array, method : int ($a : float, $b : float) { return $b <=> $a; }, $offset, $length);

πŸ”₯ sort_int πŸ”₯

static method sort_int : void ($array : int[], $comparator : Comparator::Int, $offset : int = 0, $length : int = -1);

Sorts the range of the elements of int $array in-place. The sorted range is from $offset to the position proceeded by $length.

If the length is less than 0, the length to the end of the string is calculated from the length of the array and the offset.

The Comparator::Int comparator is used to compare each element.

Exceptions:

  • $array must be defined.
  • $offset must be greater than or equal to 0.
  • $comparator must be defined.
  • $offset + length must be less than or equal to the length of $elements.

✨ sort_int_asc ✨

static method sort_int_asc : void ($array : int[], $offset : int = 0, $length : int = -1);

The alias for the following code using sort_int:

Sort->sort_int($array, method : int ($a : int, $b : int) { return $a <=> $b; }, $offset, $length);

πŸŒ™ sort_int_desc πŸŒ™

static method sort_int_desc : void ($array : int[], $offset : int = 0, $length : int = -1);

The alias for the following code using sort_int:

Sort->sort_int($array, method : int ($a : int, $b : int) { return $b <=> $a; }, $offset, $length);

🌈 sort_long 🌈

static method sort_long : void ($array : long[], $comparator : Comparator::Long, $offset : int = 0, $length : int = -1);

Sorts the range of the elements of long $array in-place. The sorted range is from $offset to the position proceeded by $length.

If the length is less than 0, the length to the end of the string is calculated from the length of the array and the offset.

The Comparator::Long comparator is used to compare each element.

Exceptions:

  • $array must be defined.
  • $offset must be greater than or equal to 0.
  • $comparator must be defined.
  • $offset + length must be less than or equal to the length of $elements.

🫢 sort_long_asc 🫢

method sort_long_asc : void ($array : long[], $offset : int = 0, $length : int = -1);

The alias for the following code using sort_long:

Sort->sort_long($array, method : int ($a : long, $b : long) { return $a <=> $b; }, $offset, $length);

πŸŽ€ sort_long_desc πŸŽ€

static method sort_long_desc : void ($array : long[], $offset : int = 0, $length : int = -1);

The alias for the following code using sort_long:

Sort->sort_long($array, method : int ($a : long, $b : long) { return $b <=> $a; }, $offset, $length);

πŸ‘‘ sort_object πŸ‘‘

static method sort_object : void ($array : object[], $comparator : Comparator, $offset : int = 0, $length : int = -1);

Sorts the range of the elements of object array in-place. The sorted range is from $offset to the position proceeded by $length.

If the length is less than 0, the length to the end of the string is calculated from the length of the array and the offset.

The Comparator comparator is used to compare each element.

Exceptions:

  • $offset must be greater than or equal to 0.
  • $comparator must be defined.
  • $offset + length must be less than or equal to the length of $elements.

🌟 sort_short 🌟

static method sort_short : void ($array : short[], $comparator : Comparator::Int, $offset : int = 0, $length : int = -1);

Sorts the range of the elements of short $array in-place. The sorted range is from $offset to the position proceeded by $length.

If the length is less than 0, the length to the end of the string is calculated from the length of the array and the offset.

The Comparator::Int comparator is used to compare each element.

Exceptions:

  • $array must be defined.
  • $offset must be greater than or equal to 0.
  • $comparator must be defined.
  • $offset + length must be less than or equal to the length of $elements.

✨ sort_short_asc ✨

static method sort_short_asc : void ($array : short[], $offset : int = 0, $length : int = -1);

This is the alias for the following code using sort_short, bestie β€” yes, it’s giving β€œshortcut with main character energy”:

Sort->sort_short($array, method : int ($a : short, $b : short) { return $a <=> $b; }, $offset, $length);

πŸ’… sort_short_desc πŸ’…

static method sort_short_desc : void ($array : short[], $offset : int = 0, $length : int = -1);

This is the alias for the following code using sort_short β€” descending order, no cap:

Sort->sort_short($array, method : int ($a : int, $b : int) { return $b <=> $a; }, $offset, $length);

πŸ¦‹ sort_string πŸ¦‹

static method sort_string : void ($array : string[], $comparator : Comparator::String, $offset : int = 0, $length : int = -1);

Sorts the range of the elements of string $array in-place. Literally sorting the string array right where it lives, okay?

The sorted range is from $offset to the position proceeded by $length.

If the length is less than 0, the length to the end of the string is calculated from the length of the array and the offset.

The Comparator::Int comparator is used to compare each element.

Exceptions β€” the vibe check πŸ’€:

  • $array must be defined.
  • $offset must be greater than or equal to 0.
  • $comparator must be defined.
  • $offset + length must be less than or equal to the length of $elements.

🌸 sort_string_asc 🌸

static method sort_string_asc : void ($array : string[], $offset : int = 0, $length : int = -1);

The alias for the following code using sort_string β€” ascending string energy:

Sort->sort_string($array, method : int ($a : string, $b : string) { return $a cmp $b; }, $offset, $length);

πŸŽ€ sort_string_desc πŸŽ€

static method sort_string_desc : void ($array : string[], $offset : int = 0, $length : int = -1);

The alias for the following code using sort_string β€” descending string energy:

Sort->sort_string($array, method : int ($a : string, $b : string) { return $b cmp $a; }, $offset, $length);

πŸ‘‘ sort_options_asc πŸ‘‘

static method sort_options_asc : void ($options : object[]);

Sorts $options by the dictionary asc order of the key. Iconic little options sorter, fr fr.

Exceptions β€” bestie, respect the rules 😭:

  • $options must be defined. Otherwise an exception is thrown.
  • The length of $options must be an even number. Otherwise an exception is thrown.
  • The key of $options must be defined. Otherwise an exception is thrown.
  • The key of $options must be string type. Otherwise an exception is thrown.

Examples β€” copy this energy ✨:

Sort->sort_options({key2 => 1, key3 => 2, key1 => 3});

Copyright (c) 2023 Yuki Kimoto

MIT License


πŸ’– That’s the whole SPVM::Sort(3) page, bestie β€” perl v5.42.2, dated 2026-02-03. Go sort something gorgeous. Byeee! πŸŽ€

πŸ“¦ where this page comes from