[GEN] custom_sort by ELEMENT of sequence?

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

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

Search



Quick Links

User menu

Not signed in.

Misc Menu