Skip to content

Instantly share code, notes, and snippets.

@anonur
Last active March 25, 2019 11:01
Show Gist options
  • Save anonur/404033fa2dafabbde6ca752d300ecb91 to your computer and use it in GitHub Desktop.
Save anonur/404033fa2dafabbde6ca752d300ecb91 to your computer and use it in GitHub Desktop.
Number comparison by sort() function
#!/usr/bin/perl
#num_sort.plx
use warnings;
use strict;
my @unsorted = (1, 2, 11, 24, 3, 36, 40, 4);
my @string = sort { $a cmp $b } @unsorted; #with cmp, we compare $a and $b variables.
print "String sort: @string\n";
my @number = sort { $a <=> $b } @unsorted; # <=> helps us to sorting numbers.
print "Numeric sort: @number\n";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment