org.jdom2.Element.sortChildren()方法的使用及代码示例

x33g5p2x  于2022-01-19 转载在 其他  
字(1.7k)|赞(0)|评价(0)|浏览(140)

本文整理了Java中org.jdom2.Element.sortChildren()方法的一些代码示例,展示了Element.sortChildren()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Element.sortChildren()方法的具体详情如下:
包路径:org.jdom2.Element
类名称:Element
方法名:sortChildren

Element.sortChildren介绍

[英]Sort the child Elements of this Element using a mechanism that is safe for JDOM content. Other child content will be unaffected. See the notes on #sortContent(Filter,Comparator) for how the algorithm works.

Collections#sort(List,Comparator) is not appropriate for sorting the Lists returned from Element#getContent() because those are 'live' lists, and the Collections.sort() method uses an algorithm that adds the content in the new location before removing it from the old. This creates validation issues with content attempting to attach to a parent before detaching first.

This method provides a safe means to conveniently sort the content.
[中]使用对JDOM内容安全的机制对该元素的子元素进行排序。其他子内容将不受影响。有关算法的工作原理,请参见#sortContent(过滤器、比较器)上的注释。
集合#sort(List,Comparator)不适用于对从元素#getContent()返回的列表进行排序,因为这些列表是“活动”列表,而集合。sort()方法使用一种算法,在将内容从旧位置删除之前,将内容添加到新位置。这会导致内容在第一次分离之前尝试附加到父级时出现验证问题。
此方法为方便地对内容进行排序提供了一种安全的方法。

代码示例

代码示例来源:origin: org.mycore/mycore-mods

public static void sort(Element mods) {
  mods.sortChildren(MCRMODSSorter::compare);
}

代码示例来源:origin: org.codehaus.izpack/izpack-util

private static void sortRootChildrenRecursive(Element root, Comparator<Element> comparator)
  {
    for (Element element : root.getChildren())
    {
      sortRootChildrenRecursive(element, comparator);
    }

    root.sortChildren(comparator);
  }
}

代码示例来源:origin: org.mycore/mycore-user2

private static void sortAttributes(Document userXML) {
  Element attributes = userXML.getRootElement().getChild("attributes");
  if (attributes == null) {
    return;
  }
  attributes.sortChildren(Comparator.comparing(o -> o.getAttributeValue("name")));
}

相关文章

微信公众号

最新文章

更多