Home > database >  How can I sort this list by column which I need to print to a .csv
How can I sort this list by column which I need to print to a .csv

Time:10-30

[1167234, 'Apple ', 'phone', 534, datetime.datetime(2022, 2, 1, 0, 0), '']
[2390112, 'Dell', 'laptop', 799, datetime.datetime(2022, 7, 2, 0, 0), '']
[9034210, 'Dell', 'tower', 345, datetime.datetime(2022, 5, 27, 0, 0), '']
[7346234, 'Lenovo', 'laptop', 239, datetime.datetime(2022, 9, 1, 0, 0), 'damaged']
[3001265, 'Samsung', 'phone', 1200, datetime.datetime(2023, 12, 1, 0, 0), '']
[2347800, 'Apple ', 'laptop', 999, datetime.datetime(2022, 7, 3, 0, 0), '']
[1009453, 'Lenovo', 'tower', 599, datetime.datetime(2023, 10, 1, 0, 0), '']

This is my code

for ids in ID:
    x = str(list((ids,ManDict.get(ids),typeDict.get(ids),PriceDict.get(ids),ServiceDatesDict.get(ids),DamagedDict.get(ids))))
    sortedX = sorted(x,key=lambda a:a[1])
    print(sortedX)
    yikes.write(x)
    yikes.write('\n')

I need to sort by column 2. I have been stuck at this for hours and tried using multiple methods such as lambda. However, I don't think I have the correct syntax for it.

CodePudding user response:

  1. Put your arrays in another array

  2. Sort the list once you're done adding all arrays

arrayToSort = []
for ids in ID:
    x = list((ids,ManDict.get(ids),typeDict.get(ids),PriceDict.get(ids),ServiceDatesDict.get(ids),DamagedDict.get(ids)))
    arrayToSort.push(x)
arrayToSort.sort(key=lambda a:a[1])

print(arrayToSort)
  • Related