Mar-20-2019, 03:05 PM
(This post was last modified: Mar-20-2019, 03:05 PM by stillsen. Edited 1 time in total.)

Hi,

I want to sort a list of lists according to multiple criteria, in a way that similar values are treated as equals.

Let's get into details.

There is a list that contains up to 12 lists with values (x, y, r).

So far so easy.

1) sort ~ y

2) sort x

i need something like:

Does anybody has an idea, how to sort such lists of lists with first criteria according to similar values of y and than secondly by x values?

Thanks a lot!

I want to sort a list of lists according to multiple criteria, in a way that similar values are treated as equals.

Let's get into details.

There is a list that contains up to 12 lists with values (x, y, r).

circles = [[536, 565, 326], [2132, 578, 323], [1296, 590, 321], [2108, 1408, 326], [ 509, 1418, 324], [1290, 1450, 324], [2134, 2269, 321], [1320, 2300, 323], [ 534, 2320, 318], [2138, 3137, 318], [ 485, 3145, 321], [1331, 3145, 326]]I want to sort the list according to the first criteria y and than according to the second x.

So far so easy.

circles.sort(key = lambda x: (x[1],x[0]))The Problem is, that for some 3 sublists y values are similar but not identical, though for sorting I need them to be treated as equals, such that I can sort according to the second criteria x.

1) sort ~ y

2) sort x

i need something like:

[ x, y, r] [536, 565, 326], [1296, 590, 321], [2132, 578, 323], [509, 1418, 324], [1290, 1450, 324], [2108, 1408, 326]My first idea was to sort the list firstly by y and than secondly sort it's sublists by x.

circles[0:3].sort(key = lambda x: x[0])But it doesn't seem to work?!

Does anybody has an idea, how to sort such lists of lists with first criteria according to similar values of y and than secondly by x values?

Thanks a lot!