# Shuffling table rows using PGF

Does anyone know how to get table with rows randomly shuffled (using \pgfplotstabletypeset from .dat-type file)?

My current code is

\documentclass{article}
\usepackage{filecontents,pgfplotstable}
\begin{document}
\begin{filecontents}{myfile.dat} %% <-- create simple .dat-file (the real one is bigger!)
A & B & C
q & w & e
r & t & y
\end{filecontents}
\pgfplotstabletypeset[
col sep=&,
string type]{myfile.dat} %% <-- here should be shuffled rows
\end{document}


Here is a dirty one: we can say that you want to "sort" the rows in an unpredictable way. So

• tell PGFPLOTSTABLE to sort the rows

• mess up PGFPLOTSTABLE's function of comparing rows.

Now we end up with a messily sorted table.

\documentclass{article}
\usepackage{filecontents,pgfplotstable}
\begin{document}
\begin{filecontents}{myfile.dat}
A & B & C
q & w & e
r & t & y
1 & 2 & 3
4 & 5 & 6
7 & 8 & 9
\end{filecontents}

\pgfkeys{
/pgfplots/float </.style={%
/pgfplots/iflessthan/.code args={##1##2##3##4}{%
\pgfmathrnd
\ifdim\pgfmathresult pt<0.5pt
##3%
\else
##4%
\fi
}%
}
}
\pgfmathsetseed{287252}
\pgfplotstabletypeset[col sep=&,string type,sort]{myfile.dat}
\end{document}


# A Saver way

As @percusse suggests, one need not redefine float < -- just define a new comparing function with a new name. The output is the same as above.

\pgfkeys{
/pgfplots/random/.style={%
/pgfplots/iflessthan/.code args={##1##2##3##4}{%
\pgfmathrnd
\ifdim\pgfmathresult pt<0.5pt
##3%
\else
##4%
\fi
}%
},
/pgfplots/table/sort cmp=random
}

• You can make a random integer sequence, add it as a column and sort on that. At least you can define a new sort cmp Jan 13 '16 at 3:12