%%
%% This is file `csvsort.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% csvtools.dtx  (with options: `csvsort.sty,package')
%% Copyright (C) 2007 Nicola Talbot, all rights reserved.
%% If you modify this file, you must change its name first.
%% You are NOT ALLOWED to distribute this file alone. You are NOT
%% ALLOWED to take money for the distribution or use of either this
%% file or a changed version, except for a nominal charge for copying
%% etc.
%% \CharacterTable
%%  {Upper-case    \A\B\C\D\E\F\G\H\I\J\K\L\M\N\O\P\Q\R\S\T\U\V\W\X\Y\Z
%%   Lower-case    \a\b\c\d\e\f\g\h\i\j\k\l\m\n\o\p\q\r\s\t\u\v\w\x\y\z
%%   Digits        \0\1\2\3\4\5\6\7\8\9
%%   Exclamation   \!     Double quote  \"     Hash (number) \#
%%   Dollar        \$     Percent       \%     Ampersand     \&
%%   Acute accent  \'     Left paren    \(     Right paren   \)
%%   Asterisk      \*     Plus          \+     Comma         \,
%%   Minus         \-     Point         \.     Solidus       \/
%%   Colon         \:     Semicolon     \;     Less than     \<
%%   Equals        \=     Greater than  \>     Question mark \?
%%   Commercial at \@     Left bracket  \[     Backslash     \\
%%   Right bracket \]     Circumflex    \^     Underscore    \_
%%   Grave accent  \`     Left brace    \{     Vertical bar  \|
%%   Right brace   \}     Tilde         \~}
\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{csvsort}[2007/07/03 v1.0 (NLCT)]
\@ifundefined{compare}{\input{compare}}{}
\newif\if@csv@ishigher
\newcommand{\csv@alphaascendingcompare}[2]{%
\@csv@extractkey#1{\@csv@keya}%
\@csv@extractkey#2{\@csv@keyb}%
\csvsort@verbose{Comparing \@csv@keya\space and \@csv@keyb}%
\edef\@csvsrt@result{\noexpand\compare{\@csv@keya}{\@csv@keyb}}%
\ifnum\@csvsrt@result=1\relax
   \@csv@ishighertrue
\else
   \@csv@ishigherfalse
\fi}
\newcommand{\csv@alphadescendingcompare}[2]{%
\@csv@extractkey#1{\@csv@keya}%
\@csv@extractkey#2{\@csv@keyb}%
\csvsort@verbose{Comparing \@csv@keya\space and \@csv@keyb}%
\edef\@csvsrt@result{\noexpand\compare{\@csv@keya}{\@csv@keyb}}%
\ifnum\@csvsrt@result=-1\relax
   \@csv@ishighertrue
\else
   \@csv@ishigherfalse
\fi}
\newcommand{\csv@numericascendingcompare}[2]{%
\@csv@extractkey#1{\@csv@keya}%
\@csv@extractkey#2{\@csv@keyb}%
\if\relax\@csv@keya\relax\def\@csv@keya{0}\fi
\if\relax\@csv@keyb\relax\def\@csv@keyb{0}\fi
\csvsort@verbose{Comparing \number\@csv@keya\space and \number\@csv@keyb}%
\ifnum\@csv@keya>\@csv@keyb\relax
   \@csv@ishighertrue
\else
   \@csv@ishigherfalse
\fi}
\newcommand{\csv@numericdescendingcompare}[2]{%
\@csv@extractkey#1{\@csv@keya}%
\@csv@extractkey#2{\@csv@keyb}%
\if\relax\@csv@keya\relax\def\@csv@keya{0}\fi
\if\relax\@csv@keyb\relax\def\@csv@keyb{0}\fi
\csvsort@verbose{Comparing \number\@csv@keya\space and \number\@csv@keyb}%
\ifnum\@csv@keya<\@csv@keyb\relax
   \@csv@ishighertrue
\else
   \@csv@ishigherfalse
\fi}
\def\@csv@extractkey#1#2#3{\def#3{#1}}
\def\@csv@extractdata#1#2#3{\def#3{#2}}
\let\csv@compare\csv@alphaascendingcompare
\RequirePackage{xkeyval}
\define@boolkey{csvsort.sty}[csvsrt]{verbose}[true]{}
\csvsrtverbosetrue
\newcommand{\csvsort@verbose}[1]{%
\ifcsvsrtverbose\typeout{#1}\fi}
\define@choicekey{csvsort.sty}{sort}[\val\nr]{alphabetical,numerical,%
alphabetical ascending,numerical ascending,%
alphabetical descending,numerical descending,%
alphabeticalascending,alphabeticaldescending,%
numericalascending,numericaldescending}{%
\ifcase\nr
  % alphabetical (ascending)
    \let\csv@compare\csv@alphaascendingcompare
  \or
  % numerical (ascending)
    \let\csv@compare\csv@numericascendingcompare
  \or
  % alphabetical ascending
    \let\csv@compare\csv@alphaascendingcompare
  \or
  % numerical ascending
    \let\csv@compare\csv@numericascendingcompare
  \or
  % alphabetical descending
    \let\csv@compare\csv@alphadescendingcompare
  \or
  % numerical descending
    \let\csv@compare\csv@numericdescendingcompare
  \or
  % alphabeticalascending
    \let\csv@compare\csv@alphaascendingcompare
  \or
  % alphabetical descending
    \let\csv@compare\csv@alphadescendingcompare
  \or
  % numericalascending
    \let\csv@compare\csv@numericascendingcompare
  \or
  % numericaldescending
    \let\csv@compare\csv@numericdescendingcompare
\fi}
\newcommand{\csv@sortvariable}{\field{1}}
\define@key{csvsort.sty}{variable}{%
\renewcommand{\csv@sortvariable}{#1}}
\newcounter{sfirstdataline}
\setcounter{sfirstdataline}{1}
\define@key{csvsort.sty}{sfirstdataline}{%
\setcounter{sfirstdataline}{#1}}
\newcounter{firstdataline}
\setcounter{firstdataline}{2}
\define@key{csvsort.sty}{firstdataline}{%
\setcounter{firstdataline}{#1}}
\ProcessOptionsX
\RequirePackage{csvtools}
\RequirePackage{xfor}
\newcommand{\csv@insertionsort}[1]{%
\let\@csv@sortedlist\relax
\@for\@csv@listelement:=#1\do{%
\if\@csv@sortedlist\relax
  \expandafter\toks@\expandafter{\@csv@listelement}%
  \edef\@csv@sortedlist{\the\toks@}%
\else
  \expandafter\@csv@insert@into\expandafter
   {\@csv@listelement}{\@csv@sortedlist}%
\fi}\let#1\@csv@sortedlist}
\newcommand\@csv@insert@into[2]{%
\let\@csv@tmplist\relax
\@for\@sort@list@element:=#2\do{%
 \expandafter\csv@compare\expandafter{\@sort@list@element}{#1}%
 \if@csv@ishigher
   \toks@{#1}%
   \if\relax\@csv@tmplist
     \edef\@csv@tmplist{\the\toks@,\@sort@list@element}%
   \else
     \edef\@csv@tmplist{\@csv@tmplist,\the\toks@,\@sort@list@element}%
   \fi
   \@endfortrue
 \else
   \if\relax\@csv@tmplist
     \edef\@csv@tmplist{\@sort@list@element}%
   \else
     \edef\@csv@tmplist{\@csv@tmplist,\@sort@list@element}%
   \fi
 \fi
}%
\if@endfor
  \ifx\@forremainder\@empty
  \else
    \edef\@csv@tmplist{\@csv@tmplist,\@forremainder}%
  \fi
\else
  \toks@{#1}%
  \if\relax\@csv@tmplist
    \edef\@csv@tmplist{\the\toks@}%
  \else
    \edef\@csv@tmplist{\@csv@tmplist,\the\toks@}%
  \fi
\fi
\@endforfalse
\let#2\@csv@tmplist
}
\newcommand{\sortapplyCSVfile}{%
\@ifstar\@ssortapplyCSVfile\@sortapplyCSVfile}
\newcommand{\@ssortapplyCSVfile}[3][]{%
\bgroup
\setkeys{csvsort.sty}{#1}%
\def\@csv@list{}%
\@sapplyCSVfile[\c@sfirstdataline]{#2}{%
\edef\csv@key{\csv@sortvariable}%
\if\relax\@csv@list\relax
\protected@xdef\@csv@list{{\csv@key}{\csvline}}%
\else
\protected@xdef\@csv@list{\@csv@list,{\csv@key}{\csvline}}%
\fi
}%
\@ssortapplyCSVdata{\@csv@list}{#3}%
\egroup}
\newcommand{\@sortapplyCSVfile}[3][]{%
\bgroup
\setkeys{csvsort.sty}{#1}%
\def\@csv@list{}%
\@applyCSVfile[\c@firstdataline]{#2}{%
\edef\csv@key{\csv@sortvariable}%
\if\relax\@csv@list\relax
\protected@xdef\@csv@list{{\csv@key}{\csvline}}%
\else
\protected@xdef\@csv@list{\@csv@list,{\csv@key}{\csvline}}%
\fi
}%
\@sortapplyCSVdata{\@csv@list}{#3}%
\egroup}
\newcommand{\sortapplyCSVdata}{%
\@ifstar\@ssortapplyCSVdata\@sortapplyCSVdata}
\newcommand{\@ssortapplyCSVdata}[3][\relax]{%
\bgroup
\if\relax#1\relax\else\setkeys{csvsort.sty}{sort=#1}\fi
\csv@insertionsort{#2}%
\c@csvrownumber=0\relax
\@for\@csv@element:=#2\do{%
\expandafter\@csv@extractdata\@csv@element\csvline
\expandafter\toks@\expandafter{\csvline}%
\edef\@csvlin@{\@csvseparator\the\toks@\@csvseparator}%
\long\edef\@csvlin@{\@csvseparator\csvline\@csvseparator}%
\c@field=0\relax
\whiledo{\not\equal{\@csvlin@}{\@csvseparator}}{%
\extractentry{\@csvlin@}{\param}%
\expandafter\toks@\expandafter{\param}%
\advance\c@field by 1\relax
\expandafter\xdef\csname @field\romannumeral\c@field\endcsname{%
\the\toks@}%
}%
\ifthenelse{\not\equal{\csvline}{}}{%
\refstepcounter{csvrownumber}%
#3}{}%
}%
\egroup
}
\newcommand{\@sortapplyCSVdata}[3][\relax]{%
\bgroup
\if\relax#1\relax\else\setkeys{csvsort.sty}{sort=#1}\fi
\csv@insertionsort{#2}%
\c@csvrownumber=0\relax
\@for\@csv@element:=#2\do{%
\expandafter\@csv@extractdata\@csv@element\csvline
\expandafter\toks@\expandafter{\csvline}%
\edef\@csvlin@{\@csvseparator\the\toks@\@csvseparator}%
\long\edef\@csvlin@{\@csvseparator\csvline\@csvseparator}%
\c@field=0\relax
\whiledo{\not\equal{\@csvlin@}{\@csvseparator}}{%
\extractentry{\@csvlin@}{\param}%
\expandafter\toks@\expandafter{\param}%
\advance\c@field by 1\relax
\expandafter\xdef\csname @field\romannumeral\c@field\endcsname{%
\the\toks@}%
\edef\@fieldlabel{%
\csname @fieldlabel\romannumeral\c@field\endcsname}%
\expandafter\xdef\csname insert\@fieldlabel\endcsname{\the\toks@}%
}%
\ifthenelse{\not\equal{\csvline}{}}{%
\refstepcounter{csvrownumber}%
#3}{}%
}%
\egroup
}
\newcommand{\sortCSVtotabular}[6][]{%
\@sortCSVtotabular[#1]{#2}{#3}{#4}{#5}{#6}{tabular}}
\newcommand{\sortCSVtolongtable}[6][]{%
\@sortCSVtotabular[#1]{#2}{#3}{#4}{#5}{#6}{longtable}}
\newcommand{\@sortCSVtotabular}[7][]{%
\bgroup
\setkeys{csvsort.sty}{#1}%
\def\@csv@list{}%
\@applyCSVfile[\c@firstdataline]{#2}{%
\edef\csv@key{\csv@sortvariable}%
\if\relax\@csv@list\relax
\protected@xdef\@csv@list{{\csv@key}{\csvline}}%
\else
\protected@xdef\@csv@list{\@csv@list,{\csv@key}{\csvline}}%
\fi
}%
\sortCSVdatatotabular{\c@csvrownumber}{\@csv@list}{#3}{#4}{#5}{#6}{#7}%
\egroup
}
\newcommand{\sortCSVdatatotabular}[7]{%
\maxlines=#1\relax
\csv@insertionsort{#2}%
\csvlastbutone=\maxlines
\advance\csvlastbutone by -1\relax
\c@csvrownumber=0\relax
\c@csvlinenum=0\relax
\def\@r@wh{#4}\def\@r@w{}\def\@r@wl{}%
\@for\@csv@element:=#2\do{%
\expandafter\@csv@extractdata\@csv@element\csvline
\expandafter\toks@\expandafter{\csvline}%
\edef\@csvlin@{\@csvseparator\the\toks@\@csvseparator}%
\long\edef\@csvlin@{\@csvseparator\csvline\@csvseparator}%
\c@field=0\relax
\whiledo{\not\equal{\@csvlin@}{\@csvseparator}}{%
\extractentry{\@csvlin@}{\param}%
\expandafter\toks@\expandafter{\param}%
\advance\c@field by 1\relax
\expandafter\xdef\csname @field\romannumeral\c@field\endcsname{%
\the\toks@}%
\edef\@fieldlabel{%
\csname @fieldlabel\romannumeral\c@field\endcsname}%
\expandafter\xdef\csname insert\@fieldlabel\endcsname{\the\toks@}%
}%
\refstepcounter{csvrownumber}%
\refstepcounter{csvlinenum}%
\ifnum\c@csvrownumber=\maxlines
  \protected@edef\@r@wl{#6}%
\else
  \expandafter\toks@\expandafter{\@r@w}
  \protected@edef\@r@w{\the\toks@#5}%
\fi
}%
\begin{#7}{#3}%
\@r@wh
\@r@w
\@r@wl
\end{#7}%
}
\endinput
%%
%% End of file `csvsort.sty'.
