1. [GEN] custom_sort by ELEMENT of sequence?

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?

Like this:  sort_on_element(sequenceA, sequenceB, elementToSortOn)

I have a sequence like this: s= { {"1","c","x1"}, {"2","a","x2"},
{"3","b","x3"},...},
and I want to sort it on a *specified* element of the sub-sequences, so that
if I specified sort on the 2nd element of the sub-sequences, I would get:
s_sorted_on_2nd_element = { {"2","a","x2"},  {"3","b","x3"},
{"1","c","x1"},...}

I understand that to do this I should use custom_sort, but because
custom_sort requires a user supplied "compare" function which is referenced
by *routine_id*, I must make a *specific* "compare" function that would look
at an interiorly specified sub-element of the sequence, like this (mod from
csort.ex):

function compare_2nd_element(sequence a, sequence b)
-- Compare two sequences according to 2nd element.
return compare(b[2], a[2])
end function

So before I go thorough trying to follow the example in "csort.e", I wonder
if there is some way to *generalize* the "custom_sort" so that one could
pass the index of the element to sort on as a parameter in the sort
function.  That way a programmer could just call the function with the
included "sort_on" parameter, rather than having to make a new "compare"
routine each time they want to do a custom compare.  However, it seems to me
that the use of routine_id makes this impossible??

If there *is* a way to do this, it seems to me it belongs in "sort.e"?

Dan Moyer

Note to Wolfgang:  this time I did check your tutorial first, but didn't see
anything about sorting.  :)

new topic     » topic index » view message » categorize

2. Re: [GEN] custom_sort by ELEMENT of sequence?

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 message » categorize

3. Re: [GEN] custom_sort by ELEMENT of sequence?

At 03:22 AM 13/01/01 -0800, you wrote:
>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

Oakey-Doaky,


global function sort_by_element(integer element, sequence x)
    --99% Sort.e RDS
    integer gap, j, first, last
    object tempi, tempj

    last = length(x)
    gap = floor(last / 3) + 1
    while 1 do
        first = gap + 1
        for i = first to last do
            tempi = x[i]
            j = i - gap
            while 1 do
                tempj = x[j]
                if compare(tempi[element],tempj[element]) >= 0 then
                    j += gap
                    exit
                end if
                x[j+gap] = tempj
                if j <= gap then
                    exit
                end if
                j -= gap
            end while
            x[j] = tempi
        end for
        if gap = 1 then
            return x
        else
            gap = floor(gap / 3) + 1
        end if
    end while
end function




----------------------------------------------------

new topic     » goto parent     » topic index » view message » categorize

4. Re: [GEN] custom_sort by ELEMENT of sequence?

Of course you can go all the way if you want:


function sort_on_element(sequence a,integer element)
    SORT_BY=element
    return custom_sort(ByElement,a)
end function


Graeme
----------------------------------------------------

new topic     » goto parent     » topic index » view message » categorize

5. 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>------------

----------------------------------------------------

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu