1. Routine for organising [ad-hoc] data, splice_sort()
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
2. Re: Routine for organising [ad-hoc] data, splice_sort()
------=_NextPart_000_0009_01C0862E.7585E260
charset="iso-8859-1"
That last post was potentially formatted so badly I have attached a file.
Also I made a small error in the final example's output.
Regards,
Aidan
------=_NextPart_000_0009_01C0862E.7585E260
name="splsort.e"
3. Re: Routine for organising [ad-hoc] data, splice_sort()
- Posted by Aidan Bindoff <abindoff at ONE.NET.AU>
Jan 24, 2001
-
Last edited Jan 25, 2001
Sorry to post again on this but I felt it useful to point out that this is
another of those "sort by nth element" routines discussed earlier, and I've
just realised that it doesn't really have an advantage over custom_sort or
the method Graeme described on the 14th. In fact, I would guess Graeme's
modified sort() would be considerably faster. I did benchmark against
custom_sort and it was slightly faster (not as convenient though), even
after doing the obvious optimisations (which didn't suit my original use).
Regards,
Aidan