Logo

Programming-Idioms

This language bar is your friend. Select your favorite languages!
  • Perl

Idiom #290 Sort sublist

Sort the part of the list items from index i (included) to index j (excluded), in place, using the comparator c.

Elements before i and after j must remain unchanged.

@items[$i..$j] = sort $c @items[$i..$j];

Use perl's list slice capability to extract the elements between $i and $j, sort them using comparator function $c, and then replace the slice in situ.
items.setRange(i, j, items.sublist(i, j)..sort(c));

New implementation...
< >
programming-idioms.org