[NTLUG:Discuss] Sort question

Fred James fredjame at fredjame.cnc.net
Thu Apr 5 09:23:31 CDT 2012


Fred wrote:
> I have a long table with 6 columns of numbers. I want each row of 6 numbers to 
>
> be arranged in ascending order and separated by some delimiter such as
> comma or space, like this:
>
> 53 17 3 24 9 33 
>
> 16 8 42 29 1 20
> <etc>
>
> would become
>
> 3 9 17 24 33 53
> 1 8 16 20 29 42
> <etc>
>
> My code was getting really stupid but that was maybe because it was
> really late. Actually, I don't know how to do this. I tried openoffice calc
> at first but it scrambled the table. I put the data into a text file but none
> of the single digit numbers have a leading zero so none of my feeble 
>
> attempts at scripting worked very well.
>
> I might mention that I also have the table in csv format instead of space
> separated, if that might help.
>
>
> Any suggestions?
>
> Thanks,
> Fred
>   

Fred
Assumption ... every row/record has 6 items of data, and every data item
is a number of >0 but <3 digits?
Language aside, as many can do this ...
    Read a record
        sort the record in ascending order
        write the record


The "sort the record in ascending order" could easily be done in an
array with a three point swap.
    c = a[i]
    if a[i+1] < a[i]
       a[i] = a[i+1]
       a[i+1] = c
If this must be done as alphabetic rather than numeric, add a leading 0
to all one digit number before sorting.

Hope that helps
Regards
Fred James

Oops ... re-reading that I realize I forgot to mention ...
(a) do that bit (for each record) within a loop
(b) loop until no more swaps occur in that record
Regards
Fred James




More information about the Discuss mailing list