1

I am trying to use key=operator.itemgetter() to sort a field of data. When I try to customize it for the 4th column it doesn't sort correctly. Here are the first sort and the result.

import csv import operator

with open('test.csv', 'r', ) as test_file:
    test_reader = csv.reader(test_file, delimiter=',')
    list = sorted(test_reader, key=operator.itemgetter(0))

    for row in list:
        print(" ".join(row))

This is the output I get with the ID field sorted correctly

1009453, Lenovo, tower, 599, 10/1/2020, 
1167234, Apple, phone, 534, 2/1/2021, 
2347800, Apple, laptop, 999, 7/3/2020, 
2390112, Dell, laptop, 799, 7/2/2020, 
3001265, Samsung, phone, 1200, 12/1/2023, 
7346234, Lenovo, laptop, 239, 9/1/2020, damaged
9034210, Dell, tower, 345, 5/27/2020, 

But when I try to sort by price:

3001265, Samsung, phone, 1200, 12/1/2023, 
7346234, Lenovo, laptop, 239, 9/1/2020, damaged
9034210, Dell, tower, 345, 5/27/2020, 
1167234, Apple, phone, 534, 2/1/2021, 
1009453, Lenovo, tower, 599, 10/1/2020, 
2390112, Dell, laptop, 799, 7/2/2020, 
2347800, Apple, laptop, 999, 7/3/2020, 

The price here goes from 1200 down to 239 then up to 345, why isn't it sorting correctly? All I am changing for get a sorted price is this list = sorted(test_reader, key=operator.itemgetter(0)) to this list = sorted(test_reader, key=operator.itemgetter(3)). The same happens when I try to sort by the date or key=operator.itemgetter(4)) the dates wouldn't be in chronological order.

Mureinik
  • 297,002
  • 52
  • 306
  • 350
  • You need to convert the price to a number. They are strings which makes them sort in alphabetical order. – Mark Dec 13 '20 at 06:15
  • 1
    Looks like they are being compared as strings and not integers. You can do `sorted(a,key=lambda x: int(x[3]))` – Equinox Dec 13 '20 at 06:18
  • @venky__ It seems like your solution worked, could you explain how lambda works? –  Dec 13 '20 at 06:43
  • 1
    @PythonStudent a lambda expression creates a function object. In this case, `lambda x: int(x[3])` is equivalent to defining a function `def f(x): return int(x[3]`). See https://stackoverflow.com/questions/16501/what-is-a-lambda-function – juanpa.arrivillaga Dec 13 '20 at 07:05
  • @juanpa.arrivillaga Ahh, thanks for the explanation, it's kinda like a placeholder –  Dec 13 '20 at 07:14
  • 1
    Please do not vandalize your posts. You can delete them by clicking the 'delete' option at the bottom. If that option is not present (which happens based on other parameters like whether it has been answered), you can contact Stack Overflow (click the 'contact us' link in the site footer) and request a post be disassociated with your account. – TylerH Dec 15 '20 at 22:29

1 Answers1

1

The prices are strings (like all columns read from a CSV), so the data gets sorted lexicographically. You need to explicitly convert them to numbers in order to sort numerically, e.g.:

lst = sorted(test_reader, key=lambda x : int(x[3]))
Mureinik
  • 297,002
  • 52
  • 306
  • 350