sort by column

Scott Woodford woodford at crystal.cirrus.com
Wed Nov 15 18:59:50 CET 2006


Thanks Joerg, nice solution.  Wayne, I've included the macro at the bottom.

Joerg Fischer wrote:

No, but I didn't understand what you had in mind because this task
should be done in the following way:

1. Make a rectangular(!) selection of the column you want to 
   sort by.

2. Start the sort_by_column macro, which is roughly


define sort_by_column {
 
 store $selection_start, $selection_end and $selection_left (= col[umn number])
 get the line start and end of the line of $selection_start and  _end, respecti
vely
 now you can select(start, end) the part of the text to be sorted and
 filter_selection("sort -k" col)
}

Cheers,
Jörg
  

define sort_by_column
{
   start = search("^", $selection_start,"regex","backward")
   end = search("$", $selection_end-2,"regex")+1
   a = search("\\S",$selection_left,"regex")
   col=choose_column(a)
   select(start,end)
   filter_selection("sort -k" col)
}

define choose_column
{
   a=$1
   b=search("^", a,"regex", "backward")
   col=0
   pos = b
   while (pos <= a)
   {
      pos = search("<\\S+>", pos+1,"regex")
      col++
   }
   return col
}

Regards,
Scott


More information about the Discuss mailing list