# Numeric sort error

**URL:** <https://community.unix.com/t/numeric-sort-error/242012>\
**Category:** Shell Programming and Scripting\
**Created:** [June 8, 2009, 10:29am UTC](https://community.unix.com/t/numeric-sort-error/242012 "2009-06-08T10:29:21Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vasuarjula](https://community.unix.com/letter_avatar/vasuarjula/32/5_5575768a8748004e209b776fc1b2916d.png) [@vasuarjula](https://community.unix.com/u/vasuarjula)\
**Post date:** [June 8, 2009, 10:29am UTC](https://community.unix.com/t/numeric-sort-error/242012/1 "2009-06-08T10:29:21Z")

</div>

Hello all

I have data like below where the column with values (PRI, SEC ) is the char field and the rest are Numeric Fields.

200707,9580,58,7,2,1,PRI,1,1,137,205594,0,5,10,-45.51,-45.51  
200707,9580,58,7,2,1,SEC,1,1,137,205594,0,5,10,-45.51,45.51  
200707,9580,58,7,2,1,PRI,1,1,138,204173,0,5,10,-58.51,-58.51  
200707,9580,58,7,2,1,SEC,1,1,138,204173,0,5,10,-58.51,58.51  
200707,9580,58,7,2,1,PRI,1,1,139,170842,0,5,10,-25.98,-25.98  
200707,9580,58,7,2,1,SEC,1,1,139,170842,0,5,10,-25.98,25.98

When i did sort on the above data using the below command:  
sort -n -t',' +0 -1 +1 -2 +2 -3 +3 -4 +4 -5 +5 -6 +6 -7 +7 -8 +8 -9 +9 -10 +10 -11

Result is like:

200707,9580,58,7,2,1,PRI,1,1,137,205594,0,5,10,-45.51,-45.51  
200707,9580,58,7,2,1,SEC,1,1,137,205594,0,5,10,-45.51,45.51  
200707,9580,58,7,2,1,PRI,1,1,138,204173,0,5,10,-58.51,-58.51  
200707,9580,58,7,2,1,SEC,1,1,138,204173,0,5,10,-58.51,58.51  
200707,9580,58,7,2,1,PRI,1,1,139,170842,0,5,10,-25.98,-25.98  
200707,9580,58,7,2,1,SEC,1,1,139,170842,0,5,10,-25.98,25.98

If you check the data, sort is screwed up, 3rd row should come before second and PRI is lesser than SEC.

But when i sort the same data using the below command

sort -t',' +0 -1n +1 -2n +2 -3n +3 -4n +4 -5n +5 -6n +6 -7 +7 -8n +8 -9n +9 -10n  
I made each field numeric specfic and PRI, SEC field left as Ascii

I got the below result which the right one

200707,9580,58,7,2,1,PRI,1,1,137,205594,0,5,10,-45.51,-45.51  
200707,9580,58,7,2,1,PRI,1,1,138,204173,0,5,10,-58.51,-58.51  
200707,9580,58,7,2,1,PRI,1,1,139,170842,0,5,10,-25.98,-25.98  
200707,9580,58,7,2,1,SEC,1,1,137,205594,0,5,10,-45.51,45.51  
200707,9580,58,7,2,1,SEC,1,1,138,204173,0,5,10,-58.51,58.51  
200707,9580,58,7,2,1,SEC,1,1,139,170842,0,5,10,-25.98,25.98

When i am sorting either numeric or ascii, why the PRI, SEC is making a difference?

---

<div class="post-metadata">

**Author:** ![Panos1962](https://community.unix.com/user_avatar/community.unix.com/panos1962/32/4704_2.png) [@Panos1962](https://community.unix.com/u/Panos1962)\
**Post date:** [June 8, 2009, 12:39pm UTC](https://community.unix.com/t/numeric-sort-error/242012/2 "2009-06-08T12:39:44Z")

</div>

Dera friend, I've also encountered the same problems in the past. I'm using the "n" qualifier for numeric fields to avoid such problems. Your example, though, has a dark point: you want numerical sort (-n) but there are non-numeric fields in the sort. Look the -k option too. Not helped a lot but it's nice to find someone to hear.
