Forums > MaxMSP

Sorting a list of lists

December 11, 2012 | 2:23 am

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?


December 11, 2012 | 3:35 am

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:

– Pasted Max Patch, click to expand. –

M


December 11, 2012 | 8:45 am

Hi,

I made an object called [sadam.sortLists] a while ago (see http://cycling74.com/forums/topic.php?id=42930 ) 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


December 11, 2012 | 11:59 am

… or you can use the list processing facilities of the bach library: http://www.bachproject.net
cheers!
aa


December 12, 2012 | 3:12 am

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.


December 12, 2012 | 4:27 am

slight modification:
sort 1 instead of sort 1 1 and renumber before dump–

– Pasted Max Patch, click to expand. –

December 12, 2012 | 4:37 am

Thanks Terry! I thought that extra 1 might be a typo.

What does "renumber" do? The code seems to work without that message.


December 12, 2012 | 5:29 am

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) ….


Viewing 8 posts - 1 through 8 (of 8 total)