Sorting a list of lists

Dec 11, 2012 at 2:23am

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?

#65626
Dec 11, 2012 at 3:35am

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

#236419
Dec 11, 2012 at 8:45am

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

#236420
Dec 11, 2012 at 11:59am

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

#236421
Dec 12, 2012 at 3:12am

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.

#236422
Dec 12, 2012 at 4:27am

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

– Pasted Max Patch, click to expand. –
#236423
Dec 12, 2012 at 4:37am

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

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

#236424
Dec 12, 2012 at 5:29am

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

#236425

You must be logged in to reply to this topic.