Wednesday, 14 August 2013

How to Sort an Array by Size (in feet and inches) in Perl?

How to Sort an Array by Size (in feet and inches) in Perl?

I have a perl array with the following sample values:
multiple_sizes[0] = 10' 0" x 14' 0":6342
multiple_sizes[1] = 2' 0" x 3' 0":0
multiple_sizes[2] = 4' 0" x 6' 0":829
multiple_sizes[3] = 6' 0" x 9' 0":2022
multiple_sizes[4] = 8' 0" x 10' 0":3145
multiple_sizes[5] = 9' 0" x 12' 0":4821
The object is to sort these values by ascending width. The problem is that
10' 0" is sorted before 2' 0" and also, 2' 10'' is sorted before 2' 6''.
Is there a way to have the array sorted so that the result gives an
ascending width? I was wondering if there was a way to treat the widths as
two digit numbers and if a second digit did not exist, it would be sorted
as one (i.e. 2' becomes 02' and 10' remains 10').
Any help is greatly appreciated.

No comments:

Post a Comment