Problem in sorting a text file

Hi;
I have a text file like this:

1
10
11
2
3
4
M
X
Y

When I sort it numerically using sort -n, it looks like this:

Y
X
M
1
2
3
4
10
11

However, I want it to be like this:

1
2
3
4
10
11
M
X
Y

Well, this was only part of text file. I have a very big text file that I want it to be sorted based on the 3rd column. Above is an example of my third column.
Please help me!

Hi.

For complex ordering tasks I often use the nonstandard utility msort:

#!/usr/bin/env bash

# @(#) s1	Demonstrate hybrid ordering of fields, msort.
# See: http://freshmeat.net/projects/msort

pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C msort

FILE=${1-data1}

pl " Input data file $FILE:"
cat $FILE

pl " Results of msort, hybrid ordering:"
msort -q --line --position 1,1 --comparison-type hybrid -- data1

exit 0

producing:

% ./s1

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
bash GNU bash 3.2.39
msort 8.44

-----
 Input data file data1:
b2
a1
b1
A1
y
m
x
1
10
11
2
3
4
M
X
Y

-----
 Results of msort, hybrid ordering:
1
2
3
4
10
11
A1
M
X
Y
a1
b1
b2
m
x
y

The msort utility was in the Debian repository for my system. See the URL noted in the script for other options.

Best wishes ... cheers, drl

 
sort -n input.txt | nawk '!/[0-9]/{a=a""ORS$0}/[0-9]/{print}END{print a}'

Hi.

@itkamaraj -- good solution using standard tools.

However, it looks like an empty line is being created. Was that intentional to separate the two groups?

#!/usr/bin/env bash

# @(#) user1	Demonstrate ordering of fields, sort, arrange with awk.

pe() { for _i;do printf "%s" "$_i";done; printf "\n"; }
pl() { pe;pe "-----" ;pe "$*"; }
db() { ( printf " db, ";for _i;do printf "%s" "$_i";done;printf "\n" ) >&2 ; }
db() { : ; }
C=$HOME/bin/context && [ -f $C ] && $C sort awk

FILE=${1-data1}

pl " Input data file $FILE:"
cat $FILE

pl " Results of sort, re-order with awk:"
sort -n $FILE |
awk '
!/[0-9]/	{a=a""ORS$0}
/[0-9]/	{print}
END	{print a}
'

exit 0

producing:

% ./user1 

Environment: LC_ALL = C, LANG = C
(Versions displayed with local utility "version")
OS, ker|rel, machine: Linux, 2.6.26-2-amd64, x86_64
Distribution        : Debian GNU/Linux 5.0.8 (lenny) 
bash GNU bash 3.2.39
sort (GNU coreutils) 6.10
awk GNU Awk 3.1.5

-----
 Input data file data1:
b2
a1
b1
A1
y
m
x
1
10
11
2
3
4
M
X
Y

-----
 Results of sort, re-order with awk:
A1
a1
b1
b2
1
2
3
4
10
11

M
X
Y
m
x
y

Best wishes ... cheers, drl