You can use Pandas module of Python to import your CSV file into a dataframe. You can sort the dataframe by any number of columns and then can export the sorted dataframe to a CSV file.
Here is an example. My CSV file 'test.csv' has the following data:
Name | Age | DOB |
AA | 29 | 1967 |
BB | 12 | 1867 |
CC | 45 | 1834 |
DD | 34 | 1945 |
EE | 38 | 2011 |
FF | 17 | 1745 |
GG | 22 | 1867 |
HH | 12 | 1756 |
II | 38 | 2000 |
You can try the following code to sort the CSV file. You can also use CSV module of Python instead of Pandas, but I prefer Pandas.
>>> import pandas as pd
>>> df = pd.read_csv('test.csv', sep=',') #Read CSV file and create a DataFrame
>>> df
Name Age DOB
0 AA 29 1967
1 BB 12 1867
2 CC 45 1834
3 DD 34 1945
4 EE 38 2011
5 FF 17 1745
6 GG 22 1867
7 HH 12 1756
8 II 38 2000
>>> df1=df.sort_values(by=['Age']) # sort by Age
Name Age DOB
1 BB 12 1867
7 HH 12 1756
5 FF 17 1745
6 GG 22 1867
0 AA 29 1967
3 DD 34 1945
4 EE 38 2011
8 II 38 2000
2 CC 45 1834
>>> df1=df.sort_values(by=['Age','Name']) # sort by Age and Name
Name Age DOB
1 BB 12 1867
7 HH 12 1756
5 FF 17 1745
6 GG 22 1867
0 AA 29 1967
3 DD 34 1945
4 EE 38 2011
8 II 38 2000
2 CC 45 1834
>>> df1.to_csv('test.csv',index=False) # write the sorted data to CSV
The output 'test.csv' now has the sorted data.
Name | Age | DOB |
BB | 12 | 1867 |
HH | 12 | 1756 |
FF | 17 | 1745 |
GG | 22 | 1867 |
AA | 29 | 1967 |
DD | 34 | 1945 |
EE | 38 | 2011 |
II | 38 | 2000 |
CC | 45 | 1834 |