Re: [GEN] custom_sort by ELEMENT of sequence?

new topic     » goto parent     » topic index » view thread      » older message » newer message

Graeme,

Jeeze, <slapping head>,

I guess I've tried so hard to impress upon myself that passing values to a
function by parameter is "cleaner" than by using global variables, I totally
forgot about doing it with an "external" variable!  Thanks Graeme, and
thanks also for using an *example* to explain it; even though I can
understand it in the abstract, the example does help.

But I'm still wondering if I am correct in thinking that because routine_id
is used, one *can't* pass the element number to the sort function by
parameter, and that there is *some*(?) reason that it has to be done via
routine_id?

Dan

----- Original Message -----
From: "Graeme" <graemeburke at CROSSWINDS.NET>
To: <EUPHORIA at LISTSERV.MUOHIO.EDU>
Sent: Friday, January 12, 2001 9:39 PM
Subject: Re: [GEN] custom_sort by ELEMENT of sequence?


> At 08:08 PM 12/01/01 -0800, you wrote:
> >Is there a way to sort a sequence by specifying a parameter in the
function
> >call which would specify which element of the sub-sequence to sort on?
>
>
> Try This:
>
>
>
> --------<Begin ELE_SORT.E>-------------------
>
> include sort.e
>
> global integer SORT_BY
> SORT_BY=0
>
> global function CS_byElement(sequence s1,sequence s2)
>     return compare(s1[SORT_BY],s2[SORT_BY])
> end function
>
> global constant ByElement=routine_id("CS_byElement")
>
> ----------<end>-----------------
>
>
>
> ----------<Begin TEST>EX>-------------------
> include ELE_SORT.e
> include get.e
> sequence dat
>
>
>
> dat={
>     {7,5,3,8},
>     {6,7,4,9},
>     {"hello","world",2,"B"},
>     {5,'a',44,2},
>     {5,"Smith","John",4566},
>     {4,7,1,9},
>     {0,6,8,3},
>     {"Apple",3,"fruit","Chisel"},
>     {1,2,3,4}   }
>
> procedure disp(sequence s)
>     clear_screen()
>     puts(1,s&"\n\n")
>     for x=1 to length(dat) do
>         print(1,dat[x])
>         puts(1,"\n")
>     end for
>     puts(1,"\n\nPress a Key...")
>     if wait_key() then end if
> end procedure
>
>
> disp("Unsorted")
>
> SORT_BY=1
> dat=custom_sort(ByElement,dat)
> disp("Sort By Element 1")
>
> SORT_BY=2
> dat=custom_sort(ByElement,dat)
> disp("Sort By Element 2")
>
> SORT_BY=3
> dat=custom_sort(ByElement,dat)
> disp("Sort By Element 3")
>
> SORT_BY=4
> dat=custom_sort(ByElement,dat)
> disp("Sort By Element 4")
>
> ------------<end>------------
>
> ----------------------------------------------------
> http://www.geocities.com/SiliconValley/Network/6843/
> ------------<graemeburke at crosswinds.net>------------

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu