Perl | sort() Function Last Updated : 17 Mar, 2023 Comments Improve Suggest changes Like Article Like Report sort() function in Perl is used to sort a list with or without the use of method of sorting. This method can be specified by the user in the form of subroutines or blocks. If a subroutine or block is not specified then it will follow the default method of sorting. Syntax: sort List sort block, List sort Subroutine, List Returns: sorted list as per user's requirement Example 1: Perl #!/usr/bin/perl @array1 = ("a", "d", "h", "e", "b"); print "Original Array: @array1\n"; print ("Sorted Array: ", sort(@array1)); Output:Original Array: a d h e b Sorted Array: abdeh Example 2: Use of Block to sort Perl #!/usr/bin/perl -w use warnings; use strict; # Use of Block to sort my @numeric = sort { $a <=> $b } (2, 11, 54, 6, 35, 87); print "@numeric\n"; Output:2 6 11 35 54 87 In the above code, block is used to sort because sort() function uses characters to sort the strings, but in numerical context, it is not possible to follow the same. Hence, block is used to ease the sort. Example 3: Use of Subroutine to sort Perl #!/usr/bin/perl -w use warnings; use strict; # Calling subroutine to sort numerical array my @numerical = sort compare_sort (2, 11, 54, 6, 35, 87); print "@numerical\n"; # function to compare two numbers sub compare_sort { if($a < $b) { return -1; } elsif($a == $b) { return 0; } else { return 1; } } Output:2 6 11 35 54 87Example - 4 : Mixed case Here we will see how the sort function works if we have an array of strings with mixed case (upper or lower) for the first character of each string. Perl #!/usr/bin/perl @st_arr = ("geeks","For","Geeks","Is","best","for","Coding"); @lst = sort(@st_arr); print("@lst"); OutputCoding For Geeks Is best for geeks Here we can see that the sort function first sorted the strings which has upper case letters at the beginning in ascending order (A-Z) and then it sorted the rest which has lower case letters at the beginning in ascending order (a-z). This happened due to the fact that the ASCII values of upper case letters is less than that of lowercase letters. Comment More infoAdvertise with us Next Article Perl | sort() Function C Code_Mech Follow Improve Article Tags : Perl Perl-function Perl-List-Functions Similar Reads Sorting Hash in Perl Prerequisite: Perl | Hashes Set of key/value pair is called a Hash. Each key in a hash structure are unique and of type strings. The values associated with these keys are scalar. These values can either be a number, string or a reference. A Hash is declared using my keyword. Let us consider an examp 6 min read Perl Reverse Sort Method The article focuses on discussing how to sort an array/ list which consists of integers or strings in reverse order (descending to ascending). Prerequisite: Sorting in Perl. Syntax: reverse sort @array_name; reverse sort (@array_name); This method is a combination of the reverse and sort method of P 3 min read Perl | Sorting of Arrays Perl has a built-in sort() function to sort an array of alphabets and numbers. When an array is passed to the sort() function it returns a sorted array. Syntax: sort @Array Returns: a sorted array Sorting of Arrays in Perl can be done in multiple ways: Use of ASCII values to sort an Array Use of Com 5 min read Sorting mixed Strings in Perl Sorting in Perl can be done with the use of a pre-defined function 'sort'. This function uses a quicksort algorithm to sort the array passed to it. Syntax: sort @array Returns: a sorted array Sorting of an array that contains strings in the mixed form i.e. alphanumeric strings can be done in various 4 min read Perl | le operator 'le' operator in Perl is one of the string comparison operators used to check for the equality of the two strings. It is used to check if the string to its left is stringwise less than or equal to the string to its right. Syntax: String1 le String2 Returns: 1 if left argument is less than or equal t 2 min read Perl | ge operator 'ge' operator in Perl is one of the string comparison operators used to check for the equality of the two strings. It is used to check if the string to its left is stringwise greater than or equal to the string to its right. Syntax: String1 ge String2 Returns: 1 if left argument is greater than or e 2 min read Excel SORT Function Explore the power of your Excel data with our easy-to-follow guide on Excel Sort Function. Whether youâre a beginner or an experienced professional, learning Excelâs sorting and filtering tools can enhance your workflow and provide valuable insights. This article explores each step, ensuring you can 6 min read PHP usort() Function PHP comes with a number of built-in functions that are used to sort arrays in an easier way. Here, we are going to discuss a new function usort(). The usort() function in PHP sorts a given array by using a user-defined comparison function. This function is useful in case if we want to sort the array 2 min read PHP rsort() Function The rsort() is an inbuilt function in PHP and is used to sort the array in descending order i.e, greatest to smallest. It sorts the actual array and hence changes are reflected in the array itself. The function provides us with 6 sorting types, according to which the array can be sorted. Syntax: rso 3 min read How to sort lines in text files in Linux | sort Command The sort command in Linux is used to sort a file, arranging the records in a particular order. By default, the sort command sorts file assuming the contents are ASCII. Using options in the sort command can also be used to sort numerically.sort is a command that sorts text files alphabetically, numer 7 min read Like