Re: Sorting by sequence member`
- Posted by Derek Parnell <ddparnell at bigpond.com> Oct 10, 2004
- 420 views
Tony Steward wrote: > > How can I sort the following sequence by 2nd, or 3rd member, using > nat_sort preferably. > > {{"tony","nsw","Australia"},{"George","qld","Australia"},{"Mary","ca","USA"}} > > ie: how do I sort by state or country Here is some demo code
include sort.e integer r_Sort_2 integer r_Sort_3 function Sort_2(object a, object b) if sequence(a)=0 or sequence(b)=0 then return 0 end if if length(a)<2 or length(b)<2 then return 0 end if return compare(a[2], b[2]) end function r_Sort_2 = routine_id("Sort_2") function Sort_3(object a, object b) if sequence(a)=0 or sequence(b)=0 then return 0 end if if length(a)<3 or length(b)<3 then return 0 end if return compare(a[3], b[3]) end function r_Sort_3 = routine_id("Sort_3") constant TestInput = {{"tony","nsw","Australia"}, {"Mary","ca","USA"}, {"George","qld","Australia"} } procedure DisplayTest(sequence m, sequence x) printf(1, "%s...\n",{ m }) for i = 1 to length(x) do printf(1,"%-15s,%-3s, %-20s\n", x[i]) end for end procedure DisplayTest( "Second element", custom_sort(r_Sort_2, TestInput) ) DisplayTest( "Third element", custom_sort(r_Sort_3, TestInput) )
Robert Craig: Can you supply a custom sort routine that takes a third, user-supplied value which is passed to the routine_id untouched. That would help a lot in reducing routines that are nearly the same etc.. Better for generic programming. -- Derek Parnell Melbourne, Australia