How to print csv rows in ascending order Python
<p>I am trying to read a csv file, and parse the data and return on row (start_date) only if the date is before September 6, 2010. Then print the corresponding values from row (words) in ascending order. I can accomplish the first half using the following: </p>

<pre><code>import csv

with open('sample_data.csv', 'rb') as f:

read = csv.reader(f, delimiter =',')

for row in read:

    if row[13] &lt;= '1283774400':
        print(row[13]+"\t \t"+row[16])
</code></pre>

<p>It returns the correct start_date range, and corresponding word column values, but they are not returning in ascending order which would display a message if done correctly.</p>

<p>I have tried to use the sort() and sorted() functions, after creating an empty list to populate then appending it to the rows, but I am just not sure where or how to incorporate that into the existing code, and have been terribly unsuccessful. Any help would be greatly appreciated.  </p>

<p>Just read the list, filter the list according to the <code>&lt; date</code> criteria and sort it according to the 13th row <em>as integer</em></p>

<p>Note that the common mistake would be to filter as ASCII (which may appear to work), but integer conversion is relaly required to avoid sort problems.</p>

<pre><code>import csv

with open('sample_data.csv', 'r') as f:
    read = csv.reader(f, delimiter =',')
    # csv has a title, we have to skip it (comment if no title)
    title_row = next(read)
    # read csv and filter out to keep only earlier rows
    lines = filter(lambda row : int(row[13]) &lt; 1283774400,read)

# sort the filtered list according to the 13th row, as numerical
slist = sorted(lines,key=lambda row : int(row[13]))

# print the result, including title line
for row in title_row+slist:
    #print(row[13]+"\t \t"+row[16])
    print(row)
</code></pre>

python sorting csv