Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl : Sort array element in alphabetical order

Tags:

arrays

regex

perl

I have a big array and I want to sort all the elements of the array in alphabetical order.

In a previous subroutine, the element of the array are being pushed to tc_reg array.

I have an array named @tc_lane. When I print element of the array it would look something like this

tx0_abc
rx0_fgw
ref_ghv
..

Now I want to sort this array like this,

ref_ghv
rx0_fgw
tx_abc
..
like image 673
Jigar Vaidya Avatar asked Nov 29 '25 18:11

Jigar Vaidya


2 Answers

If you want

rx0_fgw
rx10_fgw
rx2_fgw

use

my @sorted = sort @unsorted;

If you want

rx0_fgw
rx2_fgw
rx10_fgw

use

use Sort::Key::Natural qw( natsort );

my @sorted = natsort @unsorted;
like image 50
ikegami Avatar answered Dec 01 '25 18:12

ikegami


You simply need to do:

@tc_lane = sort @tc_lane;
like image 41
ysth Avatar answered Dec 01 '25 19:12

ysth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!