perl hash sort

I have a hash as below

'C1' => { 'x' => 41.9 , 'y' => 5.79999999999995}
'c2 288' => { 'x' => 428.05 , 'y' => 5.79999999999995}
'turn' => { 'x' => 493.25 , 'y' => 209.85}
'0001' => { 'x' => 530.1 , 'y' => 195.7}
'000001' => { 'x' => 235.25 , 'y' => 728.15}
'XYZ' => { 'x' => 56.65 , 'y' => 716.7}
'AS' => { 'x' => 78.7 , 'y' => 716.7}
'HOUSE' => { 'x' => 56.65 , 'y' => 708.3}
'dsf' => { 'x' => 89.75 , 'y' => 708.3}
'YIPPE' => { 'x' => 106.3 , 'y' => 708.3}
'DAY' => { 'x' => 150.45 , 'y' => 708.3}
'12' => { 'x' => 56.65 , 'y' => 699.9}
'INGG' => { 'x' => 95.25 , 'y' => 699.9}
'CORNER' => { 'x' => 122.85 , 'y' => 699.9}
'Light 049319' => { 'x' => 56.65 , 'y' => 691.6}
'(023117)' => { 'x' => 164.8 , 'y' => 678.35}
'01201' => { 'x' => 57.5 , 'y' => 5.79999999999995}

I need it to sort with x first and then y ... and then print as per x and y values key .
Please suggest if it is possible to sort an hash twice as required and then print ?

Here is an article on sorting a hash of hashes

Thanks @citaylor it will only sort with one value at a time. I want to sort my data with X first and then same data be sorted by Y.

perl -e'
      
 %h = (  
    C1 => { x => 41.9 , y => 5.79999999999995},
    "c2 288" => { x => 428.05 , y => 5.79999999999995},
    turn => { x => 493.25 , y => 209.85},
    "0001" => { x => 530.1 , y => 195.7},
    "000001" => { x => 235.25 , y => 728.15},
    XYZ => { x => 56.65 , y => 716.7},
    AS => { x => 78.7 , y => 716.7},
    HOUSE => { x => 56.65 , y => 708.3},
    dsf => { x => 89.75 , y => 708.3},
    YIPPE => { x => 106.3 , y => 708.3},
    DAY => { x => 150.45 , y => 708.3},
    12 => { x => 56.65 , y => 699.9},
    INGG => { x => 95.25 , y => 699.9},
    CORNER => { x => 122.85 , y => 699.9},
    "Light 049319" => { x => 56.65 , y => 691.6},
    "(023117)" => { x => 164.8 , y => 678.35},
    "01201" => { x => 57.5 , y => 5.79999999999995},
    );
    
    print map {
        $h{$_}{x}, " ", $h{$_}{y}, $/    
        }
        sort {
            $h{$a}{x} <=> $h{$b}{x} ||
            $h{$a}{y} <=> $h{$b}{y}
                } keys %h;
    '

With your data it outputs:

41.9 5.79999999999995
56.65 691.6
56.65 699.9
56.65 708.3
56.65 716.7
57.5 5.79999999999995
78.7 716.7
89.75 708.3
95.25 699.9
106.3 708.3
122.85 699.9
150.45 708.3
164.8 678.35
235.25 728.15
428.05 5.79999999999995
493.25 209.85
530.1 195.7

Wow that is good ... Thanks :slight_smile:

Could you let me know how I could change it to print data (first part) not x and y value ...

You mean the hash keys?

perl -le'
      
 %h = (  
    C1 => { x => 41.9 , y => 5.79999999999995},
    "c2 288" => { x => 428.05 , y => 5.79999999999995},
    turn => { x => 493.25 , y => 209.85},
    "0001" => { x => 530.1 , y => 195.7},
    "000001" => { x => 235.25 , y => 728.15},
    XYZ => { x => 56.65 , y => 716.7},
    AS => { x => 78.7 , y => 716.7},
    HOUSE => { x => 56.65 , y => 708.3},
    dsf => { x => 89.75 , y => 708.3},
    YIPPE => { x => 106.3 , y => 708.3},
    DAY => { x => 150.45 , y => 708.3},
    12 => { x => 56.65 , y => 699.9},
    INGG => { x => 95.25 , y => 699.9},
    CORNER => { x => 122.85 , y => 699.9},
    "Light 049319" => { x => 56.65 , y => 691.6},
    "(023117)" => { x => 164.8 , y => 678.35},
    "01201" => { x => 57.5 , y => 5.79999999999995},
    );
    
    print join $/,
        sort {
            $h{$a}{x} <=> $h{$b}{x} ||
            $h{$a}{y} <=> $h{$b}{y}
                } keys %h;
    '

Ouput:

C1
Light 049319
12
HOUSE
XYZ
01201
AS
dsf
INGG
YIPPE
CORNER
DAY
(023117)
000001
c2 288
turn
0001

yes That it ... Thanks :smiley: