Sorting a list of lists
I have a list composed of scores,
[ 10 4 8 20 ]
a list of values,
[ 5 4 1
0 0 4
3 5 0
9 9 2 ]
and a list of scores followed by values,
[10 5 4 1
4 0 0 4
8 3 5 0
20 9 9 2 ]
At the start of each run, there will be a different number of rows and columns (or scores and values). This is just an example.
I want to first sort the scores. I know I can do this by using [zl sort -1]. Then I want to sort the rows of the score and data list based on the score sort.
Eg.
[ 20 9 9 2
10 5 4 1
8 3 5 0
4 0 0 4 ]
Taking the indices from [zl sort], multiplying them by the number of elements, adding 1, and feeding them into [zl lookup] gets me a list of the sorted scores again. How do I get zl lookup to give me more than one element per index? If it can't, what can help me sort these rows?
TL;DR I want to sort
[10 5 4 1
4 0 0 4
8 3 5 0
20 9 9 2 ]
TO >>
[ 20 9 9 2
10 5 4 1
8 3 5 0
4 0 0 4 ]
and the number of elements can change. How?
If I were you, I'd sort the individual lists, and then dump them into the coll, and then sort the coll. Here's the basic idea:
M
Hi,
I made an object called [sadam.sortLists]
a while ago (see https://cycling74.com/forums/announce-the-sadam-library-version-2012-10-08 ) which does the trick for you, although you'd probably need to re-arrange your data a little bit (think about an OpenOffice Calc/M$ Excel like data sorting). See the object's help file for more details.
Hope that helps,
Ádám
... or you can use the list processing facilities of the bach library: www.bachproject.net
cheers!
aa
Thanks Siska and andrea, I'm going to try to use standard objects first.
mattyo, I'm trying to use your solution. The patch outputs a correctly sorted list with the original values. However, when I change any of the score values, the list is no longer sorted properly. I'm not sure what I'm missing.
For example, if I change the 10 to a 5, the patch outputs:
print: 20 9 9 2
print: 5 5 4 1
print: 8 3 5 0
print: 4 0 0 4
which is not properly sorted.
slight modification:
sort 1 instead of sort 1 1 and renumber before dump--
Thanks Terry! I thought that extra 1 might be a typo.
What does "renumber" do? The code seems to work without that message.
sort 1 1 isn't necessarily a typo-- it means sort by index I think, whereas sort 1 means sort by first data entry. the renumber means renumber the indexes from 0 again (because they get reordered when sorted). _anyway_ it's all in the coll help patcher btw (option-click on the object) ....