Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to sort an array of strings numerically?

I feel like this is a simple problem. But I am stuck and to be honest my mind is having an info overload at the moment. I am also new to Perl and I’m still trying to explore it.

So I have a variable that contains a string:

$strings =
"3A
1B
2A
5A
4B"

then I converted it into an array by splitting it:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

@split_array = split("\n", $strings);

print @split_array . "\n";

Output:

3A
1B
2A
5A
4B

Now, I want to sort this array in ascending order.

Here’s what I tried:

@sorted_array = sort @split_array;

Expected Output:

1B
2A
3A
4B
5A

but the output is still the same.

I apologize if I missed something obvious here.

Any help would be appreciated.

>Solution :

The split statement, split("", $strings); looks wrong – you are splitting on an empty string . I assume this is another typo?

This works fine for me

use strict;
use warnings;

my $strings = "3A 
100 
1B 
2A 
5A 
4B";

my @split_array = split(" ", $strings);

print "@split_array\n";

my @sorted_array = sort @split_array;
print "@sorted_array\n";

output is

3A 1B 2A 5A 4B
1B 2A 3A 4B 5A

Note that this is doing a string sort, rather than a numeric sort. It works in this instance because you are only sorting 2 character ASCII hex strings that happen to sort in proper numeric order. If the format is different, say the values contain 3 character ASCII hex, that string sort will not work properly.

For example

use strict;
use warnings;

my $strings = "3A 
100 
1B 
2A 
5A 
4B";

my @split_array = split(" ", $strings);

print "@split_array\n";

my @sorted_array = sort @split_array;

outputs

3A 100 1B 2A 5A 4B
100 1B 2A 3A 4B 5A

To fix that you need to do a proper numeric sort. That means converting the ASCII hex numbers into their binary equivalent.

use strict;
use warnings;

my $strings = "3A 
100 
1B 
2A 
5A 
4B";

my @split_array = split(" ", $strings);

print "@split_array\n";

my @sorted_array = sort @split_array;
print "@sorted_array\n";

my @numeric_sorted_array = sort { hex $a <=> hex $b } @split_array;
print "@numeric_sorted_array\n";

output is

3A 100 1B 2A 5A 4B
100 1B 2A 3A 4B 5A
1B 2A 3A 4B 5A 100

The key here is the sort { hex $a <=> hex $b } expression — this converts the ASCII hex into binary with the hex function, the uses the <=> operator to carry out a numeric sort.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading