![按键对属性列表 (`l3prop`) 进行排序](https://linux22.com/image/470549/%E6%8C%89%E9%94%AE%E5%AF%B9%E5%B1%9E%E6%80%A7%E5%88%97%E8%A1%A8%20(%60l3prop%60)%20%E8%BF%9B%E8%A1%8C%E6%8E%92%E5%BA%8F.png)
如何按键对属性列表进行排序?
有,\clist_sort
但是没有\prop_sort
功能。
clist
我的解决方法是使用属性列表的键创建一个,对排序clist
,然后创建一个新的属性列表,按照排序的顺序添加原始属性列表的项目clist
。然后将排序后的属性列表放回到原始属性列表中prop_set_eq
此解决方法是否遵循了以下概念expl3
? 这里的最佳实践是什么?
代码按预期工作(就我所知)。只有一步prop_set_eq
会导致问题:它挂起(见下面的代码)。
\documentclass{article}
\begin{document}
\ExplSyntaxOn
% create and initialize a property list "l_propertylist"
\prop_new:N \l_propertylist
\prop_set_from_keyval:Nn \l_propertylist {
b = y,
a = x,
c = z,
}
% show entries of "l_propertylist"
\par l_propertylist = \{ \prop_map_inline:Nn \l_propertylist { #1 = #2, } \}
% create a clist "l_keylist" to store the keys of "l_propertylist"
\clist_new:N \l_keylist
% function "__add_key_of_item_to_l_keylist"
\cs_new_protected:Npn \__add_key_of_item_to_l_keylist:nn #1 #2
{ \clist_put_right:Nn \l_keylist #1 }
% add keys of "l_propertylist" to "l_keylist"
\par\prop_map_function:NN \l_propertylist \__add_key_of_item_to_l_keylist:nn
% show items of "l_keylist"
\par l_keylist = \{ \clist_map_inline:Nn \l_keylist { #1 , } \}
\clist_sort:Nn \l_keylist
{ \str_compare:nNnTF { #1 } > { #2 }{ \sort_return_swapped: }{ \sort_return_same: } }
% show sorted items of "l_keylist"
\par sorted ~ l_keylist = \{ \clist_map_inline:Nn \l_keylist { #1 , } \}
% function "__add_entry_into_proplist_sorted_by_key"
\cs_new_protected:Npn \__add_entry_into_proplist_sorted_by_key:n #1
{ \prop_put:Nnn \l_propertylist_sorted_by_key
{ #1 }
{ \prop_item:Nn \l_propertylist { #1 } }
}
% create a property list "l_propertylist_sorted_by_key"
\prop_new:N \l_propertylist_sorted_by_key
% add entries of "l_propertylist" to "l_propertylist_sorted_by_key"
% selected by sorted items of "l_keylist"
\clist_map_function:NN \l_keylist \__add_entry_into_proplist_sorted_by_key:n
% show entries of "l_propertylist_sorted_by_key"
\par l_propertylist_sorted_by_key = \{ \prop_map_inline:Nn \l_propertylist_sorted_by_key { #1 = #2, } \}
% set the content of "l_propertylist" equal to that of "l_propertylist_sorted_by_key"
%\prop_set_eq:NN \l_propertylist \l_propertylist_sorted_by_key
% show entries of "l_propertylist"
\par l_propertylist = \{ \prop_map_inline:Nn \l_propertylist { #1 = #2, } \}
\ExplSyntaxOff
\end{document}
上述代码按预期输出以下内容:
l_propertylist={b=y,a=x,c=z,}
l_keylist={b,a,c,}
sorted l_keylist={a,b,c,}
l_propertylist_sorted_by_key={a=x,b=y,c=z,}
l_propertylist={b=y,a=x,c=z,}
如果该步骤prop_set_eq
处于活动状态,它将挂起(参见下面的代码):
$ xelatex sort_prop_list.tex
This is XeTeX, Version 3.141592653-2.6-0.999995 (TeX Live 2023) (preloaded format=xelatex)
restricted \write18 enabled.
entering extended mode
(./sort_prop_list.tex
LaTeX2e <2023-06-01> patch level 1
L3 programming layer <2023-10-10>
(/usr/local/texlive/2023/texmf-dist/tex/latex/base/article.cls
Document Class: article 2023/05/17 v1.4n Standard LaTeX document class
(/usr/local/texlive/2023/texmf-dist/tex/latex/base/size10.clo))
(/usr/local/texlive/2023/texmf-dist/tex/latex/l3backend/l3backend-xetex.def)
(./sort_prop_list.aux)
(/usr/local/texlive/2023/texmf-dist/tex/latex/base/ts1cmr.fd)