Sort data from column to row

Hi, I need somebody's help with sorting data with awk.
I've got a file:

        10         aaa            4584
        12         bbb            6138
        20         ccc            4417
        21         ddd            7796
        10         eee            7484
        12         fff            9503
        20         ggg            7034
        21         hhh            9652
        10         iii            4894
        12         jjj            6011
        20         kkk            8504
        21         lll            4691
        10         mmm            2487
        12         nnn            3078
        20         ooo            7903
        21         ppp            6722

I need to to sort this data to get output like this:

        10         aaa         eee         iii         mmm
        12         bbb         fff         jjj         nnn 
        20         ccc         ggg         kkk         ooo 
        21         ddd         hhh         lll         ppp

With Perl:

perl -lane'
  push @{$_{$F[0]}}, $F[1];
  END {
    print join "\t", $_, sort, @{$_{$_}} 
      for sort { $a <=> $b } keys %_
    }' infile

Which version of awk do you have?

Update:

It seams that the second column is already ordered, if this is true, that task is easier.
With GNU awk < 4 it would be:

WHINY_USERS= gawk 'END {
  for (k in _)
    printf "%d%s%s\n", k, OFS, _[k]
  }
{
  k = sprintf("%15s", $1)
  _[k] = k in _ ? _[k] OFS $2 : $2
  }' OFS='\t' infile  

With GNU awk >= 4:

awk 'END {
  PROCINFO["sorted_in"] = "@ind_str_asc"
  for (k in _)
    print k, _[k]
  }
{
  _[$1] = $1 in _ ? _[$1] OFS $2 : $2
  }' OFS='\t' infile  

For other awk implementations:

sort -k1,1n -k2,2 infile |
  awk 'END {
    for (i = 0; ++i <= c;)
      print o, _[o]
    }
  {
   t[$1]++ || o[++c] = $1
   _[$1] = $1 in _ ? _[$1] OFS $2 : $2
    }' OFS='\t'

Actually I don't know how to check the version. Is that
mawk 1.3.3-15 that you asking about?

In regards to the topic I don't need to change order of column expressions. So sorting is not necessary. Thank you I will try it.

With mawk you should use the last one.

Update:

Actually, if the second column is already ordered, this should be sufficient:

awk 'END {
    for (k in _)
      print k, _[k]
    }
  {
   _[$1] = $1 in _ ? _[$1] OFS $2 : $2
    }' OFS='\t' infile |
      sort

Thanks man! It works perfectly.

You're welcome!