Re: Sorting by sequence member`
- Posted by "Juergen Luethje" <j.lue at gmx.de> Oct 10, 2004
- 413 views
Tony Steward wrote: > I have looked at custom sort and the demo csort.exe but it just > doesn't sink in how to do it. > > Can't run csort.ex as xp complains about running dos program > > Tony > > > On Sun, 10 Oct 2004 02:10:07 -0700, irv mullins <guest at rapideuphoria.com> > wrote: >> >> posted by: irv mullins <irvm at ellijay.com> >> >> Tony Steward wrote: >>> >>> Kat your sorttok doesn't seem to like my data layout (i think) as it >>> returns nothing. >> >> >> Tony: I think custom_sort() will do what you want. Look it up in the >> docs (it's 5 a.m. here, I've been up since 2 a.m. and am too groggy to >> write a demo :) The following code is slightly modified after the code that Andy Serpa posted here on 25 Feb 2003. Very clean, straightforward, and flexible. Thanks again, Andy!
object sort_i function compare_index (object x1, object x2) -- Don't call this function directly, see below. integer comp, ix if integer(sort_i) then if sort_i > 0 then return compare(x1[sort_i], x2[sort_i]) else return -compare(x1[-sort_i], x2[-sort_i]) end if end if for j = 1 to length(sort_i) do ix = sort_i[j] if ix > 0 then comp = compare(x1[ix], x2[ix]) else comp = -compare(x1[-ix], x2[-ix]) end if if comp != 0 or j = length(sort_i) then return comp end if end for end function global function flex_sort (sequence s, object i) -- This is the function you actually call. -- s = sequence to be sorted (assumed to be at least 2-dimensional) -- i = integer index of element (column) to sort on, -- or sequence of indexes to sort on multiple fields -- (in order of preference). -- If any given index is positive, then it will sort that element in ascending order. -- If negative, it will sort that element in descending order. sort_i = i return custom_sort(routine_id("compare_index"), s) end function
Regards, Juergen -- .... there are two ways of constructing a software design: One way is to make it so simple that there are /obviously/ no deficiencies and the other is to make it so complicated that there are no /obvious/ deficiencies. [C.A.R. Hoare (1987), The Emperor's Old Clothes]