Jigar Vaidya
Jigar Vaidya

Reputation: 153

Perl : Sort array element in alphabetical order

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
..

Upvotes: 3

Views: 5721

Answers (2)

ikegami
ikegami

Reputation: 385590

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;

Upvotes: 8

ysth
ysth

Reputation: 98388

You simply need to do:

@tc_lane = sort @tc_lane;

Upvotes: 2

Related Questions