Re: Sorting of two interdependent sequences

new topic     » goto parent     » topic index » view thread      » older message » newer message
SnakeCharmer said...

There are two sequences, first for names and second for numbers. I need to sort numbers, but names should be sorted as appropriate.

Example (in case I badly explained in English):

Names = {"Army of Lovers", "Boney M.", "C. C. Catch", "Dschinghis Khan"}
Numbers = {1987, 1975, 1985, 1979}

After sorting:

Numbers = {1975, 1979, 1985, 1987}
Names = {"Boney M.", "Dschinghis Khan", "C. C. Catch", "Army of Lovers"}

May be the better way is to store names along with years as follows:

Groups = {{"Army of Lovers", 1987}, {"Boney M.", 1979}, {"C. C. Catch", 1985}, {"Army of Lovers", 1987}} 

and then apply to the sequence custom_search() function with a custom comparator function:

include std/sort.e 
 
function comparator(object x, object y) 
   integer cmp = compare(x[2],y[2]) -- compare by year 
   if cmp!=0 then 
       return cmp 
   else -- if years are identical then compare by name: 
       return compare(x[1],y[1]) 
   end if 
end function 
 
Groups = custom_sort(routine_id("comparator"), Groups) 
new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu