/***
This is the other part of the jagged column sorting macro set, this one
for number columns.
***/
// macro source starts here
// jigjagnumbsort.s - a simpler jaggedy number column sorter
<ctrlshift x><n> execmacro("jigjagnumbsort")
/***
Sometimes, simple is better.
This is a simpler, quicker version of the staggered column sorter of
earlier this year.
The macro sorts a column of numbers even if the columnis not
aligned. After the sort, the data in the column remains
in its original horizontal position on each line (not aligned).
The macro takes sort parameter on the command line, like so:
jigjagnumbsort (parameter)
like:
jigjagnumbsort -d
The default sort is ascending, but the -d parameter will sort
descending.
The command by itself defaults to ascending sort.
Assumptions:
1. column block is required;
2. sort decimals as well as whole numbers;
3. there should only be numbers, decimal points, and minus signs in the
column;
4. blank column lines will sort as normally to top or bottom depening on
the sort order chosen.
If the macro happens to be used on a text column, the resulting sort
will as though right-aligned.
***/
proc main()
integer ob=0
integer tb=0
string p[2]=Lower(Query(MacroCmdLine))
string gt[255]=""
integer i=0
integer tl=0
integer dl=0
integer nt=0
// cmdline option or ask
if p==""
p="-a"
if ask("Sort Options: (-d)escending, (-a)scending, ESC=Quit",p)
and length(p)
else
sound(40,40)
warn("Cancelled.")
goto ending
endif
endif
// set sort option
if pos("d",p)
p="-d"
else
p="-a"
endif
// start work
if isblockmarked()==_column_
gotoblockbegin()
pushposition()
ob=getbufferid()
pushblock()
// check for decimal
if lfind(".","gl")
// if one, use long method
tb=createbuffer("numb"+rightstr(str(gettime()),4)+".jigjag")
gotobufferid(tb)
copyblock()
unmarkblock()
// get longest decimal length
for i=1 to numlines()
gotoline(i)
// add one if not one
if lfind(".","cg")==0
endline()
inserttext(".0")
dl=1
endif
// get longest
gt=trim(gettext(1,currlinelen()))
nt=numtokens(gt,".")
if gt<>""
tl=length(gettoken(gt,".",nt))
if tl>dl
dl=tl
endif
endif
endfor
// make all decimals the same length
for i=1 to numlines()
gotoline(i)
gt=trim(gettext(1,currlinelen()))
nt=numtokens(gt,".")
if gt<>""
tl=length(gettoken(gt,".",nt))
if tl<dl
endline()
inserttext(format("0":-(dl-tl):"0"))
endif
endif
endfor
begfile()
markcolumn(1,1,numlines(),longestlineinbuffer())
gotobufferid(ob)
endif
popposition()
copyblock()
if tb
abandonfile(tb)
endif
// do the sort
execmacro("adjust right")
while lreplace(" -","- ","ngl") // put minus to left column in block
endwhile
execmacro("sort "+p)
delblock()
popblock()
else
sound(40,40)
warn("No Block. Ending.")
endif
sound(11000,30)
message("(jigjagnumbsort) Finished.")
goto ending
ending:
end
// macro source ends here