perl sort

Perl array/sort FAQ - Perl integer array sorting

Summary: How to sort Perl arrays, in this case, a Perl integer array.

Sorting a Perl integer array (technically a Perl numeric array) is relatively simple, well, at least once you know the magic formula. The key thing to know is that you need to provide the Perl sort function a helper function (or block of code) that tells it how to sort integers. By default, the Perl sort function sorts arrays in ASCII order, and that's not going to work very well for you.

Perl case-insensitive string array sorting

Perl string array sorting FAQ: How can I sort a Perl string array in a case-insensitive manner?

I mentioned in my first Perl array sorting tutorial that the default Perl sorting algorithm sorts characters by their ASCII values. Because of that, my simple Perl array sorting example using the following strings worked just fine:

# a simple perl string array
@pizzas = qw(pepperoni cheese veggie sausage);

However, if I add a few uppercase strings to that string array, like this:

Perl hash sort - How to sort a Perl hash by the hash key

Perl hash sort FAQ: How do I sort a Perl hash by the hash key?

Answer: Sorting the output of a Perl hash by the hash key is fairly straightforward. It involves two Perl functions, keys and sort, along with the good old foreach statement.