Sort different lists in the same sequence

Question:

I have three different stripes and I need to sort them by ‘num’

cod = []
desc = []
num =[]

cod.append("1")
desc.append("Product1")
num.append(94)

cod.append("2")
desc.append("Product2")
num.append(93)

cod.append("3")
desc.append("Product3")
num.append(95)

for row in range(len(cod)):
    print(cod[row], desc[row], num[row])

The result I want to get is this

3 Product3 95
1 Product1 94
2 Product2 93

I tried using num.sort() but it didn’t work

Asked By: Brandalize

||

Answers:

I was able to get your desired output by putting the lists into a list of lists and then sorting by the num element

cod = []
desc = []
num =[]
L = []
cod.append("1")
desc.append("Product1")
num.append(94)

cod.append("2")
desc.append("Product2")
num.append(93)

cod.append("3")
desc.append("Product3")
num.append(95)

[L.append([cod[row], desc[row], num[row]]) for row in range(len(cod))]
    
L.sort(key=lambda sublist: sublist[2], reverse=True)

[print(*i) for i in L]

Output:

3 Product3 95
1 Product1 94
2 Product2 93
Answered By: stefan_aus_hannover
cod = []
desc = []
num =[]

cod.append("1")
desc.append("Product1")
num.append(94)

cod.append("2")
desc.append("Product2")
num.append(93)

cod.append("3")
desc.append("Product3")
num.append(95)

zipped = list(zip(num, cod, desc))
zipped.sort()

num, desc, cod = zip(*zipped)

for row in range(len(cod)):
    print(cod[row], desc[row], num[row])

Output:

Product2 2 93
Product1 1 94
Product3 3 95
Answered By: Aymen

It’s clear that you want to sort (in reverse) on the values in num although the sub-sorts are unclear. However, this will give the required output:

cod = ['1', '2', '3']
desc = ['Product1', 'Product2', 'Product3']
num = [94, 93, 95]

for n, d, c in sorted(zip(num, desc, cod), reverse=True):
    print(c, d, n)

Output:

3 Product3 95
1 Product1 94
2 Product2 93
Answered By: OldBill

Managing three separate lists is cumbersome and should be avoided. I changed it up a bit and created a dataclass that encapsulates all the data per product.
The __str__ method is overloaded to produce the same result as your required output.
The sorting is the same as @stefan_aus_hannover’s but with a bit better readability (it uses the .number member instead of the index 2 of a sublist).
There is also an option to use in-place sorting of the list

from dataclasses import dataclass

@dataclass(kw_only=True)
class Product:
    code: str
    description: str
    number: int
    
    def __str__(self):
        return f'{self.code} {self.description} {self.number}'

products = [
Product(code='1',description='Product1',number=94),
Product(code='2',description='Product2',number=93),
Product(code='3',description='Product3',number=95),
]

# EITHER create a new sorted list
products = sorted(products,key=lambda p: p.number, reverse=True)
# OR sort in place
products.sort(key=lambda p: p.number, reverse=True)

for prod in products:
    print(prod)

Output:

3 Product3 95
1 Product1 94
2 Product2 93
Answered By: Gábor Fekete
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.