按行对表格进行排序

按行对表格进行排序

我有很多这样的表格:

\begin{table}[]
\centering
\caption{Osmotic down TR CC}
\resizebox{1\textwidth}{!}{
\label{my-label}
\begin{tabularx}{1.4\textwidth}{|l|Y|T{-1.4}|}
\hline
term ID    & description                                & {log10 p-value} \\ \hline
GO:0000502 & proteasome complex                         & -2.6484       \\ \hline
GO:0016020 & membrane                                   & -3.8974       \\ \hline
GO:0016021 & integral component of membrane             & -1.2830       \\ \hline
GO:0012505 & endomembrane system                        & -0.2604       \\ \hline
GO:0030479 & actin cortical patch                       & -2.4783       \\ \hline
GO:0043332 & mating projection tip                      & -1.1630       \\ \hline
GO:0005933 & cellular bud                               & -0.3994       \\ \hline
GO:0005739 & mitochondrion                              & -2.1914       \\ \hline
GO:0005737 & cytoplasm                                  & -0.3145       \\ \hline
GO:0034515 & proteasome storage granule                 & -1.4628       \\ \hline
GO:0005749 & mitochondrial respiratory chain complex II & -1.5398       \\ \hline
GO:0005628 & prospore membrane                          & -0.9696       \\ \hline
GO:0000407 & pre-autophagosomal structure               & -1.4259       \\ \hline
GO:0005773 & vacuole                                    & -1.9021       \\ \hline
GO:0019898 & extrinsic component of membrane            & -0.7193       \\ \hline
GO:1990112 & RQC complex                                & -0.9441       \\ \hline
GO:1990143 & CoA-synthesizing protein complex           & -0.7790       \\ \hline
GO:0005759 & mitochondrial matrix                       & -0.5562       \\ \hline
GO:0035859 & Seh1-associated complex                    & -0.2316       \\ \hline
GO:1990429 & peroxisomal importomer complex             & -0.6132       \\ \hline
GO:0000329 & fungal-type vacuole membrane               & -1.2879       \\ \hline
GO:0034657 & GID complex                                & -0.6000       \\ \hline
\end{tabularx}}
\end{table}

我想根据列的值自动对行进行排序log10 p-value。我希望选项卡显示第一行对应数字最小的行,log10 p-value最后一行对应数字最大的行。

这可能吗?

答案1

这里有一个datatool实现读取 CSV 表,对其进行排序,并通过tabularx

在此处输入图片描述

\documentclass{article}

\usepackage{datatool,filecontents,tabularx,booktabs}

\begin{document}

\begin{filecontents*}{term-log.csv}
%termID    , description                               , logten
GO:0000502, proteasome complex                        , -2.6484
GO:0016020, membrane                                  , -3.8974
GO:0016021, integral component of membrane            , -1.2830
GO:0012505, endomembrane system                       , -0.2604
GO:0030479, actin cortical patch                      , -2.4783
GO:0043332, mating projection tip                     , -1.1630
GO:0005933, cellular bud                              , -0.3994
GO:0005739, mitochondrion                             , -2.1914
GO:0005737, cytoplasm                                 , -0.3145
GO:0034515, proteasome storage granule                , -1.4628
GO:0005749, mitochondrial respiratory chain complex II, -1.5398
GO:0005628, prospore membrane                         , -0.9696
GO:0000407, pre-autophagosomal structure              , -1.4259
GO:0005773, vacuole                                   , -1.9021
GO:0019898, extrinsic component of membrane           , -0.7193
GO:1990112, RQC complex                               , -0.9441
GO:1990143, CoA-synthesizing protein complex          , -0.7790
GO:0005759, mitochondrial matrix                      , -0.5562
GO:0035859, Seh1-associated complex                   , -0.2316
GO:1990429, peroxisomal importomer complex            , -0.6132
GO:0000329, fungal-type vacuole membrane              , -1.2879
GO:0034657, GID complex                               , -0.6000
\end{filecontents*}

\DTLloaddb[noheader,keys={termID,description,logten}]{termlog}{term-log.csv}
\DTLsort{logten=ascending}{termlog}

\noindent
\begin{tabularx}{\linewidth}{ l X r }
  \toprule
  \bfseries term ID & 
    \bfseries description & 
    \bfseries $\log_{10}$ $p$-value \\
  \midrule
  \DTLforeach{termlog}{%
    \termID=termID,\termdesc=description,\logten=logten}{%
    \termID & \termdesc & $\logten$ \\
  }
  \\[-\normalbaselineskip] \bottomrule
\end{tabularx}

\end{document}

答案2

我复制了\mergesort欧佩克宏包用于排序。你可以试试这个:

\documentclass{article}

\long\def\addto#1#2{\expandafter\def\expandafter#1\expandafter{#1#2}}
\def\sdef#1{\expandafter\def\csname#1\endcsname}
\def\removeiilist{\gdef\iilist{}}

\def\mergesort #1#2,#3{% by Miroslav Olsak
   \ifx,#1%                      % prazdna-skupina,neco,  (#2=neco #3=pokrac)
      \addto\iilist{#2,}%        % dvojice skupin vyresena
      \return{\fif\mergesort#3}%    % \mergesort pokracovani
   \fi
   \ifx,#3%                      % neco,prazna-skupina,  (#1#2=neco #3=,)
      \addto\iilist{#1#2,}%      % dvojice skupin vyresena
      \return{\fif\mergesort}%      % \mergesort dalsi
   \fi
   \ifx\end#3%                   % neco,konec (#1#2=neco)
      \ifx\empty\iilist                % neco=kompletni setrideny seznam
         \def\iilist{#1#2}%
         \return{\fif\fif\gobbletoend}%   % koncim
      \else                      % neco=posledni skupina nebo \end
         \return{\fif\fif \expandafter\removeiilist % spojim \iilist+neco znova
                \expandafter\mergesort\iilist#1#2,#3}%
   \fi\fi                      % zatriduji: p1+neco1,p2+neco2,
   \isAleB #1#3\ifAleB         % p1<p2
      \addto\iilist{#1}%       % p1 do bufferu
      \return{\fif\mergesort#2,#3}%         % \mergesort neco1,p2+neco2,
   \else                       % p1>p2
      \addto\iilist{#3}%       % p2 do bufferu
      \return{\fif\mergesort#1#2,}%         % \mergesort p1+neco1,neco2,
   \fi
   \relax % zarazka, na ktere se zastavi \return
}
\def\return#1#2\fi\relax{#1} \def\fif{\fi}
\def\gobbletoend #1\end{}
\def\isAleB#1#2#3{\ifnum#1<#2}

\newcount\trnum

\def\trstart{\par \trnum=0 \def\iilist{}}
\def\tr #1&#2&#3.#4\\{\advance\trnum by1
   \sdef{tr:\the\trnum}{#1&#2&#3.#4\\\hline}%
   \sdef{trs:\the\trnum}{#3#4}%
   \expandafter\addto\expandafter\iilist\expandafter{%
      \csname trs:\the\trnum\endcsname,}%
}
\def\rewriteiilist#1{\ifx#1\relax \else 
   \expandafter\rewriteiilistA\string#1\relax \expandafter\rewriteiilist\fi}
\def\rewriteiilistA#1:#2\relax{%
   \expandafter\addto\expandafter\iilist\expandafter{\csname tr:#2\endcsname}}
\def\trsort{%
   \expandafter\removeiilist \expandafter\mergesort \iilist \end,\end
   \expandafter\removeiilist \expandafter\rewriteiilist \iilist \relax
}

\begin{document}

\trstart
\tr GO:0000502 & proteasome complex                         & -2.6484       \\
\tr GO:0016020 & membrane                                   & -3.8974       \\
\tr GO:0016021 & integral component of membrane             & -1.2830       \\
\tr GO:0012505 & endomembrane system                        & -0.2604       \\
\tr GO:0030479 & actin cortical patch                       & -2.4783       \\
\tr GO:0043332 & mating projection tip                      & -1.1630       \\
\tr GO:0005933 & cellular bud                               & -0.3994       \\
\tr GO:0005739 & mitochondrion                              & -2.1914       \\
\tr GO:0005737 & cytoplasm                                  & -0.3145       \\
\tr GO:0034515 & proteasome storage granule                 & -1.4628       \\
\tr GO:0005749 & mitochondrial respiratory chain complex II & -1.5398       \\
\tr GO:0005628 & prospore membrane                          & -0.9696       \\
\tr GO:0000407 & pre-autophagosomal structure               & -1.4259       \\
\tr GO:0005773 & vacuole                                    & -1.9021       \\
\tr GO:0019898 & extrinsic component of membrane            & -0.7193       \\
\tr GO:1990112 & RQC complex                                & -0.9441       \\
\tr GO:1990143 & CoA-synthesizing protein complex           & -0.7790       \\
\tr GO:0005759 & mitochondrial matrix                       & -0.5562       \\
\tr GO:0035859 & Seh1-associated complex                    & -0.2316       \\
\tr GO:1990429 & peroxisomal importomer complex             & -0.6132       \\
\tr GO:0000329 & fungal-type vacuole membrane               & -1.2879       \\
\tr GO:0034657 & GID complex                                & -0.6000       \\
\trsort
\begin{tabular}{|l|l|l|}
\hline
term ID    & description                           & {log10 p-value} \\ \hline
\iilist
\end{tabular}

\end{document}

编辑如果您不想对 LaTeX 源文件做任何重大更改,那么您可以将\SORTmyTAB宏定义添加到上面的其他定义中:

\def\SORTmyTAB#1\hline#2\hline{%
   \def\trhead{#2}%
   \trstart
   \trx
}
\def\trx#1{\ifx\end#1\expandafter\trend \else
   \expandafter\trxA\expandafter#1\fi
}
\def\trxA#1\hline{\tr#1\unskip\trx}
\def\trend#1{%
   \trsort
   \begin{tabular}{|l|l|r|}  % Normal LaTeX construction, maybe differ
   \hline
   \trhead \hline
   \iilist
   \end{tabular}
}

并且你可以只将\SORTmyTAB控制序列放在表格环境之前。例如:

\begin{table}[]
\centering
\caption{Osmotic down TR CC}
\resizebox{1\textwidth}{!}{%
\label{my-label}%
\SORTmyTAB
\begin{tabularx}{1.4\textwidth}{|l|Y|T{-1.4}|}
\hline
term ID    & description                                & {log10 p-value} \\ \hline
GO:0000502 & proteasome complex                         & -2.6484       \\ \hline
GO:0016020 & membrane                                   & -3.8974       \\ \hline
GO:0016021 & integral component of membrane             & -1.2830       \\ \hline
GO:0012505 & endomembrane system                        & -0.2604       \\ \hline
GO:0030479 & actin cortical patch                       & -2.4783       \\ \hline
GO:0043332 & mating projection tip                      & -1.1630       \\ \hline
GO:0005933 & cellular bud                               & -0.3994       \\ \hline
GO:0005739 & mitochondrion                              & -2.1914       \\ \hline
GO:0005737 & cytoplasm                                  & -0.3145       \\ \hline
GO:0034515 & proteasome storage granule                 & -1.4628       \\ \hline
GO:0005749 & mitochondrial respiratory chain complex II & -1.5398       \\ \hline
GO:0005628 & prospore membrane                          & -0.9696       \\ \hline
GO:0000407 & pre-autophagosomal structure               & -1.4259       \\ \hline
GO:0005773 & vacuole                                    & -1.9021       \\ \hline
GO:0019898 & extrinsic component of membrane            & -0.7193       \\ \hline
GO:1990112 & RQC complex                                & -0.9441       \\ \hline
GO:1990143 & CoA-synthesizing protein complex           & -0.7790       \\ \hline
GO:0005759 & mitochondrial matrix                       & -0.5562       \\ \hline
GO:0035859 & Seh1-associated complex                    & -0.2316       \\ \hline
GO:1990429 & peroxisomal importomer complex             & -0.6132       \\ \hline
GO:0000329 & fungal-type vacuole membrane               & -1.2879       \\ \hline
GO:0034657 & GID complex                                & -0.6000       \\ \hline
\end{tabularx}}
\end{table}

我没有评论无法使用的用法\resizebox、表格的视觉质量等。我只回答了你的问题,并简单地在这里复制了你的代码。

相关内容