You do not need a loop, I think.
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.3}
\pgfplotstableread[col sep=comma]{
Function,Proc1,Proc2,Proc3
Add,1,2,3
Sub,3,4,4
Div,2,3,5
}{\data}
% sort the table
\pgfplotstablesort[
create on use/sum/.style={
create col/expr={
\thisrow{Proc1} + \thisrow{Proc2} + \thisrow{Proc3}
},
},
sort cmp=float >,
sort key=sum,
]{\dataSorted}{\data}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
cycle list=Dark2,
ybar stacked,
ymin=0,
xtick=data,
xtick pos=left,
ytick pos=left,
axis lines=left,
xticklabels from table={\dataSorted}{Function},
x tick label style={rotate=45, anchor=north east, inner sep=0mm},
ylabel=Elapsed seconds,
xlabel=Function,
legend pos=outer north east,
legend style={draw=none},
enlarge x limits=0.1,
]
\pgfplotstablegetcolsof{\dataSorted}
\pgfmathtruncatemacro\numberofcols{\pgfplotsretval-1}
\foreach \i in {1,...,\numberofcols} {
\addplot table [y index=\i, x expr=\coordindex] {\dataSorted};
}
\end{axis}
\end{tikzpicture}
\end{document}

The lower part of my answer is borrowed from this answer.
You could also use
\pgfplotstablesort[
create on use/sum/.style={
create col/expr={
\thisrowno{1} + \thisrowno{2} + \thisrowno{3}
},
},
sort cmp=float >,
sort key=sum,
]{\dataSorted}{\data}