View on GitHub

CSV-Sorter

A Java command-line CSV sorting program.

Download this project as a .zip file Download this project as a tar.gz file

CSV-Sorter

Overview

The csvsorterprogram serves to sort CSV files.

It is a Java command-line program which processes one CSV input file to one CSV output file controlled by an XML configuration file. Depending on the configuration, csvsorter can deal with different formats, separators, delimiters, various sorting presets, header and no header files.

The csvsorter program was developed as external sorting program for the csvsimple LaTeX-package. But it can be used for any CSV sorting task.

Example

Consider the following CSV file named songcontest.csv:

Title,Artist,Country,Televote,Juryvote
La la la la la,The Singers,United Kingdom,25,62
One and two and three,Hansi Unterober,Germany,47,35
Rarara,Channel Rats,Grand Fenwick,12,14
The green hills of the shire,Frodo and Friends,New Zealand,76,45
Paris e Calais,Chantal and Pascal,France,47,41
The bell rings,Baab,Sweden,87,24

This CSV file is to be sorted by the sum of the Televoteand Juryvotenumbers in descending order. If candidates have the same values, the Country is used for sorting.

For this, the following XML configuration file sumsort.xml is used:

<?xml version="1.0" encoding="UTF-8"?>
<csv>
  <sortlines>
    <sum order="descending" type="integer">
      <column name="Televote"/>
      <column name="Juryvote"/>
    </sum>
    <column name="Country" order="ascending" type="string"/>
  </sortlines>
</csv>

Now, csvsorter is applied by

csvsorter -c sumsort.xml -i songcontest.csv -o songcontest_sorted.csv

The result is the sorted file songcontest_sorted.csv:

Title,Artist,Country,Televote,Juryvote
The green hills of the shire,Frodo and Friends,New Zealand,76,45
The bell rings,Baab,Sweden,87,24
Paris e Calais,Chantal and Pascal,France,47,41
La la la la la,The Singers,United Kingdom,25,62
One and two and three,Hansi Unterober,Germany,47,35
Rarara,Channel Rats,Grand Fenwick,12,14

Example (continued for LaTeX)

The example is continued with a demonstration of on-the-fly sort in LaTeX documents using the csvsimple package.

Instead of sorting the songcontest.csv example beforehand, it can be sorted on-the-fly while compiling a LaTeX document:

% \documentclass{article}
% \usepackage{array,booktabs,csvsimple}
% \begin{document}

\csvreader[sort by=sumsort.xml,
  head to column names,
  tabular=lllcc,
  table head=\toprule\textbf{Artist} & \textbf{Title} & \textbf{Country} &
             \textbf{Points} & \textbf{TV+JV}\\\midrule,
  table foot=\bottomrule]
{songcontest.csv}{}
{\Artist & \itshape''\Title'' & \Country &
 \bfseries\the\numexpr\Televote+\Juryvote\relax & $(\Televote+\Juryvote)$}

% \end{document}

The result after compilation with (pdf)latex is:

Table

License

This application is licensed under the 3-Clause ("New") BSD license.