Routine for organising [ad-hoc] data, splice_sort()
- Posted by Aidan Bindoff <abindoff at ONE.NET.AU> Jan 24, 2001
- 453 views
Hello, here is a function splice_sort() which some of you may find useful. If you find any bugs, or think of any improvements, please let me know. I use it for organising ad-hoc data, e.g stock market data files. It might also be useful for event based programs. e.g x={{event,time},{event,time},{event,time}} y={{event,time},{event,time}} z={{event,time},{event,time}} log=splice_sort(x&y&z,2) -- to organise events in time series include sort.e function splice_sort(object string,integer pos) sequence temp if pos=1 then -- this section is just string=sort(string) -- an optimisation return string -- end if -- if pos>length(string[1]) then -- check that pos return -1 -- is valid end if -- i.e not > than sequence -- n.b could be added to each -- iteration of loop below for -- better error reporting, but slower temp=string for i=1 to length(string) do temp[i]=string[i][pos]&string[i] -- add element[pos] to end for -- first pos, temp=sort(temp) -- sort, for i=1 to length(temp) do -- temp[i]=temp[i][2..length(temp[i])] -- then clean up end for return temp -- known bug: if elements sorted are equal, it sorts next element etc... end function example: output=splice_sort(file1&file2,2) -- output={{"CCA",19930329,7.19},{"CCA",19930330,7.23}, {"CCA",19930402,7.25},{"CCA",19930402,7.28}} This has sorted data from file1 and file2 by date (element 2). Regards, Aidan