Re: [GEN] custom_sort by ELEMENT of sequence?

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

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 thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu