%%
%% This is file `arraysort.sty',
%% generated with the docstrip utility.
%%
%% The original source files were:
%%
%% arraysort.dtx  (with options: `package')
%% 
%% This is a generated file.
%% 
%% Copyright (C) 2013 by Robert J Lee
%% 
%% This file may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either
%% version 1.2 of this license or (at your option) any later
%% version. The latest version of this license is in:
%% 
%%    http://www.latex-project.org/lppl.txt
%% 
%% and version 1.2 or later is part of all distributions of
%% LaTeX version 1999/12/01 or later.
%% 
\NeedsTeXFormat{LaTeX2e}[1999/12/01]
\ProvidesPackage{arraysort}
   [2013/09/04 v1.0 sorting arrayjobx arrays]
\newcommand*{\arraysort@extrapkgs}{}
\DeclareOption{comparestr}{
  \g@addto@macro{\arraysort@extrapkgs}{
    \RequirePackage{pdftexcmds}% for comparison. TODO: use compare.sty optionally
  }
  \newcommand*{\arraysortcomparestr}[2]{%
    \protected@edef\arraysort@left{#1}%
    \protected@edef\arraysort@right{#2}%
    \arraysort@comparestr%
  }
  \newcommand*{\arraysort@comparestr}{%
    \protected@edef\arraysort@compresult{\pdf@strcmp{\arraysort@left}{\arraysort@right}}%
    \ifthenelse{\equal{\arraysort@compresult}{0}}{%
      \toggletrue{arraysortresequal}%
    }{%
      \togglefalse{arraysortresequal}%
      \ifthenelse{\equal{\arraysort@compresult}{-1}}{%
        \toggletrue{arraysortresult}% #2 > #1
      }{%
        \togglefalse{arraysortresult}% #2 < #1
      }%
    }%
  }
}
\DeclareOption{comparenum}{
  \newcommand*{\arraysortcomparenum}[2]{%
    \ifthenelse{\equal{#1}{#2}}{%
      \toggletrue{arraysortresequal}%
    }{%
      \togglefalse{arraysortresequal}%
      \ifthenelse{#2 > #1}{%
        \toggletrue{arraysortresult}%
      }{%
        \togglefalse{arraysortresult}%
      }%
    }%
  }
}
\newcommand*{\sortArrayPartitionMed}[4]{%
  \setcounter{arraysort@temp1}{(#2 + #3) / 2}%
  \edef\arraysort@midpos{\arabic{arraysort@temp1}}%
  \testarray{#4}(#2)\protected@edef\arraysort@left{\temp@macro}%
  \testarray{#4}(\arraysort@midpos)\protected@edef\arraysort@mid{\temp@macro}%
  \testarray{#4}(#3)\protected@edef\arraysort@right{\temp@macro}%
  \csname#1\endcsname{\arraysort@left}{\arraysort@mid}%
  \iftoggle{arraysortresequal}{%
  }{%
    \iftoggle{arraysortresult}{%
      \csname#1\endcsname{\arraysort@left}{\arraysort@right}%
      \iftoggle{arraysortresequal}{%
      }{%
        \iftoggle{arraysortresult}{%
          \csname#1\endcsname{\arraysort@mid}{\arraysort@right}%
          \iftoggle{arraysortresequal}{%
          }{%
            \iftoggle{arraysortresult}{%
              \arraysort@swap{#4}{#2}{\arraysort@midpos}%
            }{%
              \arraysort@swap{#4}{#2}{#3}%
            }%
          }%
        }{%
        }%
      }%
    }{%
      \csname#1\endcsname{\arraysort@mid}{\arraysort@right}%
      \iftoggle{arraysortresequal}{%
      }{%
        \iftoggle{arraysortresult}{%
          \arraysort@swap{#4}{#2}{#3}%
        }{%
          \arraysort@swap{#4}{#2}{\arraysort@midpos}%
        }%
      }%
    }%
  }%
  \sortArrayPartitionFirst{#1}{#2}{#3}{#4}%
}
\DeclareOption{randompart}{
  \g@addto@macro{\arraysort@extrapkgs}{
    \RequirePackage[quiet]{lcg}
  }
  \newcommand*{\sortArrayPartitionRand}[4]{%
    \reinitrand[counter=arraysort@temp1,first=#2,last=#3,quiet=y]%
    \rand%
    \arraysort@swap{#4}{#2}{\arabic{arraysort@temp1}}%
    \sortArrayPartitionFirst{#1}{#2}{#3}{#4}%
  }
}
\newcommand*{\sortArrayPartitionMid}[4]{%
  \setcounter{arraysort@temp1}{(#2 + #3) / 2}%
  \arraysort@swap{#4}{#2}{\arabic{arraysort@temp1}}%
  \sortArrayPartitionFirst{#1}{#2}{#3}{#4}%
}
\newcommand*{\sortArrayPartitionFirst}[4]{%
  \setcounter{arraysort@partpos}{#2}%
  \setcounter{arraysort@temp1}{#2 + 1}%
  \setcounter{arraysort@endpos}{#3 + 1}%
  \arraysort@repeats{arraysort@temp1}{\value{arraysort@temp1}}{\value{arraysort@endpos}}{1}{%
    \testarray{#4}(\arabic{arraysort@temp1})%
    \let\arraysort@cur\temp@macro%
    \testarray{#4}(\arabic{arraysort@partpos})%
    \protected@edef\arraysort@left{\arraysort@cur}%
    \protected@edef\arraysort@right{\temp@macro}%
    \csname#1\endcsname{\arraysort@left}{\arraysort@right}% #2 = cur, #3 = partition
    \setcounter{arraysort@temp2}{\value{arraysort@partpos} + 1}%
    \iftoggle{arraysortresequal}{% #2 = #3
      \arraysort@swap{#4}{\arabic{arraysort@partpos}}{\arabic{arraysort@temp2}}%
      \arraysort@swap{#4}{\arabic{arraysort@partpos}}{\arabic{arraysort@temp1}}%
      \stepcounter{arraysort@partpos}%
    }{%
      \iftoggle{arraysortresult}{% #3 > #2
        \ifthenelse{\equal{\arabic{arraysort@temp2}}{\arabic{arraysort@temp1}}}{%
          \arraysort@swap{#4}{\arabic{arraysort@partpos}}{\arabic{arraysort@temp1}}%
        }{%
          \arraysort@swap{#4}{\arabic{arraysort@partpos}}{\arabic{arraysort@temp2}}%
          \arraysort@swap{#4}{\arabic{arraysort@partpos}}{\arabic{arraysort@temp1}}%
        }%
        \stepcounter{arraysort@partpos}%
      }{%
      }%
    }%
  }%
}
\ProcessOptions\relax
\RequirePackage{arrayjobx}
\RequirePackage{calc}
\RequirePackage{ifthen}
\RequirePackage{etoolbox}
\RequirePackage{xargs}
\RequirePackage{macroswap}
\arraysort@extrapkgs
\let\arraysort@extrapkgs\relax
\newcommandx*\sortArray[5][1=arraysortcomparestr,2=1,5=sortArrayPartitionMed]{%
  \ifcsname#1\endcsname%
  \ifthenelse{#2>0}{%
    \ifthenelse{#3>#2}{%
      \ifcsname total@#4\endcsname%
        \arraysort@sort{#1}{#2}{#3}{#4}{#5}%
      \else%
        \PackageError{arraysort}{Cannot sort unknown array #4}{}%
      \fi%
    }{%
    \PackageError{arraysort}{Cannot sort; to index #3 greater than from index #2}{}%
    }%
  }{%
    \PackageError{arraysort}{Cannot sort; Invalid from index #2}{}%
  }%
  \else%
    \PackageError{arraysort}{Cannot sort by undefined comparator #1}{}%
  \fi%
}
\newcommand*{\arraysort@sort}[5]{%
  \csname#5\endcsname{#1}{#2}{#3}{#4}%
  \edef\arraysort@partition{\value{arraysort@partpos}}%
  \setcounter{arraysort@temp1}{\arraysort@partition - 1}%
  \ifthenelse{#2 = \value{arraysort@temp1} \OR #2 > \value{arraysort@temp1}}{%
  }{%
    \edef\arraysort@to{\arabic{arraysort@temp1}}%
    \arraysort@sort{#1}{#2}{\arraysort@to}{#4}{#5}%
  }%
  \setcounter{arraysort@temp1}{\arraysort@partition + 1}%
  \ifthenelse{\value{arraysort@temp1} = #3 \OR #3 < \value{arraysort@temp1}}{%
  }{%
    \edef\arraysort@from{\arabic{arraysort@temp1}}%
    \arraysort@sort{#1}{\arraysort@from}{#3}{#4}{#5}%
  }%
}
\newcounter{arraysort@partpos}
\newcounter{arraysort@temp1}
\newcounter{arraysort@temp2}
\newcounter{arraysort@endpos}
\newtoggle{arraysortresult}
\newtoggle{arraysortresequal}
\newcommand*{\arraysort@repeats}[5]{%
  \setcounter{#1}{#2}%
  \ifthenelse{\equal{\value{#1}}{#3}}{%
  }{%
    #5%
    \addtocounter{#1}{#4}%
    \arraysort@repeats{#1}{\arabic{#1}}{#3}{#4}{#5}%
  }%
}
\newcommand\arraysort@swap[3]{%
  \gmacroswap{#1#2\string~}{#1#3\string~}%
}
\endinput
%%
%% End of file `arraysort.sty'.
