sorting csv file based on column selected

Hi all,

in my csv file it'll look like this, and of course it may have more columns

US to UK;abc-hq-jcl;multimedia
UK to CN;def-ny-jkl;standard
DE to DM;abc-ab-klm;critical
FD to YM;la-yr-tym;standard
HY to MC;la-yr-ytm;multimedia
GT to KJ;def-ny-jrt;critical

I would like to group them based on the 3rd column first, then sort them based on 2nd column so it'll look like this

DE to DM;abc-ab-klm;critical
GT to KJ;def-ny-jrt;critical
US to UK;abc-hq-jcl;multimedia
HY to MC;la-yr-ytm;multimedia
UK to CN;def-ny-jkl;standard
FD to YM;la-yr-tym;standard

it would also be nice if somehow I can make it look like this so it'll look more presentable :P,

CRITICAL
DE to DM;abc-ab-klm;critical
GT to KJ;def-ny-jrt;critical

Multimedia
US to UK;abc-hq-jcl;multimedia
HY to MC;la-yr-ytm;multimedia

Standard
UK to CN;def-ny-jkl;standard
FD to YM;la-yr-tym;standard

hmmm use sort command

sort -t";" -k3 -k2 filename

and for second req use awk..

sort -t";" -k3 -k2 filename|awk -F\; '{A[$3]=A[$3]"\n"$0}END{for (i in A){print toupper(i)A}}'

Using awk rule idea.

ff=file.txt

sort -t";" -k3 -k2 $ff | \
   awk -F";" -v keyfld=3 '
      # - next key value
      $keyfld != prevkey { 
                print toupper($keyfld) 
                }
      # - every line
                { 
                print $0
                prevkey=$keyfld
                }
   '

Or using ksh

prevkey=""
oifs="$IFS"
typeset -u key
sort -t";" -k3 -k2 $ff | while read line
do
        IFS=";"
        set -A flds -- $line    # to array, 1st index 0 
        IFS="$oifs"
        key=${flds[2]}
        [ "$prevkey" != "$key" ] && print "$key"
        print "$line"
        prevkey=$key
done

Because you said

A slight modification to vidyadhar85's code

sort -t";" -k3 -k2 filename|awk -F\; '{A[$NF]=A[$NF]"\n"$0}END{for (i in A) {print toupper(i)A}}'

how about below perl:

my @arr=<DATA>;
print map {$_->[0]}
      sort {$a->[1]->[2] cmp $b->[1]->[2] or $a->[1]->[1] cmp $b->[1]->[1]}
      map {my @tmp=split(";",$_);[$_,\@tmp]}
      @arr;
__DATA__
US to UK;abc-hq-jcl;multimedia
UK to CN;def-ny-jkl;standard
DE to DM;abc-ab-klm;critical
FD to YM;la-yr-tym;standard
HY to MC;la-yr-ytm;multimedia
GT to KJ;def-ny-jrt;critical