1. Sorting of two interdependent sequences
- Posted by SnakeCharmer Aug 21, 2012
- 1116 views
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"}
2. Re: Sorting of two interdependent sequences
- Posted by ChrisB (moderator) Aug 21, 2012
- 1086 views
Hi
I don't there is a one stage function for this in eu.
I think you would have to do it in several stages 1. Sort by date 2. Find multiple date sets 3. Within each multiple sort by name.
See the manual for sort
Chris
3. Re: Sorting of two interdependent sequences
- Posted by SnakeCharmer Aug 21, 2012
- 1087 views
Thanks, nice idea! My situation is even simpler. I shouldn't have identical values in the array of numbers. By the way, whether there is a simple way to make sure for it? For example, something similar to "uniq" UNIX command?
include sort.e sequence Names = {"Army of Lovers", "Boney M.", "C. C. Catch", "Dschinghis Khan"} sequence Numbers = {1987, 1975, 1985, 1979} sequence SortedNumbers = sort(Numbers) sequence SortedNames = {} for i = 1 to length(Numbers) do SortedNames = append(SortedNames, Names[find(SortedNumbers[i], Numbers)]) end for for i = 1 to length(SortedNames) do puts(1, SortedNames[i]) puts(1, "\n") end for
4. Re: Sorting of two interdependent sequences
- Posted by Insolor Aug 21, 2012
- 1065 views
- Last edited Aug 22, 2012
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)
5. Re: Sorting of two interdependent sequences
- Posted by DerekParnell (admin) Aug 21, 2012
- 1095 views
There are two sequences, first for names and second for numbers. I need to sort numbers, but names should be sorted as appropriate.
include std/sort.e sequence Names = {"Army of Lovers", "Boney M.", "C. C. Catch", "Dschinghis Khan"} sequence Numbers = {1987, 1975, 1985, 1979} function sorter(sequence A, sequence B) sequence x x = repeat({0,0}, length(A)) for i = 1 to length(x) do x[i][1] = A[i] x[i][2] = B[i] end for x = sort(x) for i = 1 to length(x) do A[i] = x[i][1] B[i] = x[i][2] end for return {A,B} end function sequence t t = sorter(Numbers, Names) t = sorter(Names, Numbers)
6. Re: Sorting of two interdependent sequences
- Posted by petelomax Aug 21, 2012
- 1171 views
I recently faced a similar problem at work. A program like Windows Explorer but MDI, so you could open say C:\Downloads and H:\Archive and drag and drop between the two. I wanted a single copy of the data (which could be directories or database records or whatever) but there was nothing to stop someone opening C:\Downloads in two windows and sorting either by Name/Date/Size/whatever. The solution was a tag sort; each window owned their own tag array but the underlying data remained completely unchanged however they sorted it. Create an array of indexes and sort that, then instead of referencing things via [i], simply reference them via [tag[i]].
sequence Names = {"Army of Lovers", "Boney M.", "C. C. Catch", "Dschinghis Khan"} sequence Numbers = {1987, 1975, 1985, 1979} sequence tags tags = repeat(0,length(Names)) for i=1 to length(tags) do tags[i] = i end for function by_year(integer i, integer j) integer res res = compare(Numbers[i],Numbers[j]) if res=0 then res = compare(Names[i],Names[j]) end if return res end function tags = custom_sort(routine_id("by_year"),tags) for i=1 to length(tags) do printf(1,"Year: %d, Name: %s\n",{Numbers[tags[i]],Names[tags[i]]}) end for
You may need "include sort.e" or "include std/sort.e" at the top. It is trivial to write other custom sort routines, eg by_name would be the same as by_year but with the compare instructions swapped.
Pete
7. Re: Sorting of two interdependent sequences
- Posted by EUWX Aug 21, 2012
- 1003 views
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"}
{1987, 1975, 1985, 1979}
convert it to
{1987.1, 1975.2, 1985.3, 1979.4}
Sort it to
{1975.2, 1979.4, 1985.3, 1987.1}
extract a new sequence from this
{0.2, 0.4, 0.3, 0.1}
multiply by 10 to
{2, 4 ,3, 1}
Use this sequence of integers to extract the elements of the other sequence
extract the 2nd, then 4th then 3rd then 1st from
{"Army of Lovers", "Boney M.", "C. C. Catch", "Dschinghis Khan"}
and you get
{"Boney M.", "Dschinghis Khan", "C. C. Catch", "Army of Lovers" }
If your intial integer sequence (of years) is long - say 73 elements, you can add 0.01, 0.02,.... etc to 0.73 and so on and so forth.