If you have access to a Linux box then use sort
as suggested above. But, if it has to be Java then at least use an existing library to parse the CSV file. The format is hellishly complicated to parse if you want to handle all the corner cases correctly. I'd suggest a library like OpenCSV.
This code snippet show how to use the library (with all error handling omitted!)
/**
* Sorts a CSV file by a fixed column.
*
* @param col The zero-based column to sort by.
* @param in The input CSV file.
* @param out The output writer to receive the reordered CSV.
*/
public static void sort(final int col, final Reader in, final Writer out)
throws IOException {
final List<String[]> csvContent = new ArrayList<String[]>();
// parse CSV file
final CSVReader reader = new CSVReader(in);
String[] line;
while ((line = reader.readNext()) != null) {
csvContent.add(line);
}
reader.close();
// sort CSV content
Collections.sort(csvContent, new Comparator<String[]>() {
@Override
public int compare(final String[] o1, final String[] o2) {
// adjust here for numeric sort, etc.
return o1[col].compareTo(o2[col]);
}
});
// write sorted content
final CSVWriter writer = new CSVWriter(out);
writer.writeAll(csvContent);
writer.close();
}
You can adjust the code to handle different separator characters, quote chars, numeric sorting, etc.