排序Java集合(Sort Java Collection)
我有一个Java集合:
Collection<CustomObject> list = new ArrayList<CustomObject>();
CustomObject
在显示列表之前有一个id
字段,我想通过该id
对这个集合进行排序。有没有办法我这样做?
I have a Java collection:
Collection<CustomObject> list = new ArrayList<CustomObject>();
CustomObject
has anid
field now before display list I want to sort this collection by thatid
.Is there any way I could that do that?
原文:https://stackoverflow.com/questions/6957631
最满意答案
一种可能的方案:
dl.xpath('dt').each_with_index do |dt, i| dds = dt.xpath("following-sibling::dd[not(../dt[#{i + 2}]) or " + "following-sibling::dt[1]=../dt[#{i + 2}]]") puts "#{dt.text}: #{dds.map(&:text).join(', ')}" end
这依赖于
dt
元素的值比较,并且当存在重复时将失败。 以下(更复杂的)表达式不依赖于唯一的dt
值:following-sibling::dd[not(../dt[$n]) or (following-sibling::dt[1] and count(following-sibling::dt[1]|../dt[$n])=1)]
注意:您对
self
使用失败,因为您没有正确使用它作为轴(self::
。 另外,self
总是只包含上下文节点,因此它将引用由表达式检查的每个dd
,而不是返回到原始dt
One possible solution:
dl.xpath('dt').each_with_index do |dt, i| dds = dt.xpath("following-sibling::dd[not(../dt[#{i + 2}]) or " + "following-sibling::dt[1]=../dt[#{i + 2}]]") puts "#{dt.text}: #{dds.map(&:text).join(', ')}" end
This relies on a value comparison of
dt
elements and will fail when there are duplicates. The following (much more complicated) expression does not depend on uniquedt
values:following-sibling::dd[not(../dt[$n]) or (following-sibling::dt[1] and count(following-sibling::dt[1]|../dt[$n])=1)]
Note: Your use of
self
fails because you're not properly using it as an axis (self::
). Also,self
always contains just the context node, so it would refer to eachdd
inspected by the expression, not back to the originaldt
相关问答
更多-
你可以这样做: ../node[not(text()) and preceding-sibling::node[@id][1][@id='1']] 其中'1'是当前节点的ID(动态生成表达式)。 表达说: 从当前的情况转到父母 选择那些子节点 没有文字和 从所有“具有一个id的兄弟节点之前”,第一个必须有一个id 如果你在XSLT中,你可以选择下面的兄弟轴,因为你可以使用current()函数: