I'm trying to set up a certain data structure in my program that takes the data from an excel file. It needed to be sorted by the following criteria:
- Accending order by index 5.
- If two structures have the same value for index 5, put the one which has index 0 = 8 to be listed first.
Currently, I've been able to sort by accending order by index 5 to turn this:
[8, 8, 8, 8, 0, 250]
[8, 8, 8, 0, 0, 50]
[8, 8, 0, 0, 0, 5]
[1, 1, 1, 1, 1, 50]
[1, 1, 1, 1, 0, 25]
[1, 1, 1, 0, 0, 4]
[2, 2, 2, 2, 2, 60]
[2, 2, 2, 2, 0, 30]
[2, 2, 2, 0, 0, 5]
[3, 3, 3, 3, 3, 70]
[3, 3, 3, 3, 0, 35]
[3, 3, 3, 0, 0, 8]
[4, 4, 4, 4, 4, 80]
[4, 4, 4, 4, 0, 40]
[4, 4, 4, 0, 0, 10]
[5, 5, 5, 5, 5, 90]
[5, 5, 5, 5, 0, 45]
[5, 5, 5, 0, 0, 12]
[6, 6, 6, 6, 6, 100]
[6, 6, 6, 6, 0, 50]
[6, 6, 6, 0, 0, 15]
[9, 9, 9, 9, 9, 120]
[9, 9, 9, 9, 0, 60]
[9, 9, 9, 0, 0, 20]
[9, 9, 0, 0, 0, 2]
[10, 10, 10, 10, 10, 150]
[10, 10, 10, 10, 0, 75]
[10, 10, 10, 0, 0, 25]
[10, 10, 0, 0, 0, 3]
[11, 11, 11, 11, 11, 400]
[11, 11, 11, 11, 0, 150]
[11, 11, 11, 0, 0, 40]
[11, 11, 0, 0, 0, 3]
into this
[11, 11, 11, 11, 11, 400]
[8, 8, 8, 8, 0, 250]
[11, 11, 11, 11, 0, 150]
[10, 10, 10, 10, 10, 150]
[9, 9, 9, 9, 9, 120]
[6, 6, 6, 6, 6, 100]
[5, 5, 5, 5, 5, 90]
[4, 4, 4, 4, 4, 80]
[10, 10, 10, 10, 0, 75]
[3, 3, 3, 3, 3, 70]
[9, 9, 9, 9, 0, 60]
[2, 2, 2, 2, 2, 60]
[6, 6, 6, 6, 0, 50]
[1, 1, 1, 1, 1, 50]
[8, 8, 8, 0, 0, 50]
[5, 5, 5, 5, 0, 45]
[11, 11, 11, 0, 0, 40]
[4, 4, 4, 4, 0, 40]
[3, 3, 3, 3, 0, 35]
[2, 2, 2, 2, 0, 30]
[10, 10, 10, 0, 0, 25]
[1, 1, 1, 1, 0, 25]
[9, 9, 9, 0, 0, 20]
[6, 6, 6, 0, 0, 15]
[5, 5, 5, 0, 0, 12]
[4, 4, 4, 0, 0, 10]
[3, 3, 3, 0, 0, 8]
[2, 2, 2, 0, 0, 5]
[8, 8, 0, 0, 0, 5]
[1, 1, 1, 0, 0, 4]
[11, 11, 0, 0, 0, 3]
[10, 10, 0, 0, 0, 3]
[9, 9, 0, 0, 0, 2]
This uses the .sort(key = lambda x:x[5])
and .reverse()
functions.
However, drawing attention to one section in particular.
[6, 6, 6, 6, 0, 50]
[1, 1, 1, 1, 1, 50]
[8, 8, 8, 0, 0, 50]
I would like to anything with an 8 at index 0 to be the first listed, so this should be
[8, 8, 8, 0, 0, 50]
[6, 6, 6, 6, 0, 50]
[1, 1, 1, 1, 1, 50]
I'm aware that in this specific case, this could be accomplished by doing a secondary sort on index 0 to get the desired result. However, there may be cases with different inputs where we may have, say
[6, 6, 6, 6, 0, 50]
[1, 1, 1, 1, 1, 50]
[8, 8, 8, 0, 0, 50]
[11, 11, 0, 0, 0, 50]
which would need to be sorted to
[8, 8, 8, 0, 0, 50]
[6, 6, 6, 6, 0, 50]
[1, 1, 1, 1, 1, 50]
[11, 11, 0, 0, 0, 50]
So doing a sort on a secondary variable wouldn't work. Where can i start to look for a solution to this particular problem?