Sorting Awk generalized array

Generalized arrays take any type of variable(s) as subscripts, but the subscript(s) are treated as one long string expression.
The use of for(a in x) on a generalized array will return all of the valid subscripts in some order, not necessarily the one you wished.

How can I make it so that i get the items returned sorted ...... ?

Hi ,

Could you just share your code..or some sample.!!!!!
..

Thanks
Sha

The order is somewhat random and may vary among the many implementations of awk.
This is an example how to to print the array in the order it's created. It uses a helper array b to hold the order of the main array (use nawk or /usr/xpg4/bin/awk on Solaris if you get errors):

awk '{a[$1]=$0;b[++n]=$1}
END{for(i=1;i<=n;i++)print a[b]}' file

With this example it's also possible to access a specific number of an element of the array, for instance, to print the 5th element:

awk '{a[$1]=$0;b[++n]=$1}
END{print a[b[5]]}' file

And this example shows how you can sort an array with the external sort command if you don't have gawk:

awk '{a[$1]=$0}END{for(i in a)print a|"sort"}' file

Regards