- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Sorting Arrays in Perl
The sort() function in Perl sorts each element of an array according to the ASCII Numeric standards. This function has the following syntax −
Syntax
sort [ SUBROUTINE ] LIST
This function sorts the LIST and returns the sorted array value. If SUBROUTINE is specified then specified logic inside the SUBROUTINE is applied while sorting the elements.
Example
#!/usr/bin/perl # define an array @foods = qw(pizza steak chicken burgers); print "Before: @foods\n"; # sort this array @foods = sort(@foods); print "After: @foods\n";
Output
This will produce the following result −
Before: pizza steak chicken burgers After: burgers chicken pizza steak
Please note that sorting is performed based on the ASCII Numeric value of the words. So the best option is to first transform every element of the array into lowercase letters and then perform the sort function.
- Related Articles
- Merging Arrays in Perl
- Perl Sequential Number Arrays
- Transform Perl Strings into Arrays
- Transform Perl Arrays to Strings
- Sorting arrays by two criteria in JavaScript
- Sorting arrays using bubble sort in JavaScript
- How to compare two arrays for equality in Perl?
- Sorting an integer without using string methods and without using arrays in JavaScript
- Sorting Elements of Arrays and Wrapper Classes that Already Implements Comparable in Java
- Comments in Perl
- Whitespaces in Perl
- Dereferencing in Perl
- Sorting in C++
- "Here" Documents in Perl
- Escaping Characters in Perl

Advertisements