void (@NotNull XmlFile xmlFile) { XmlDocument document = xmlFile.getDocument(); if (document == null) { return; } XmlTag rootTag = document.getRootTag(); if (rootTag == null) { return; } List<XmlTag> itemTags = new ArrayList<>(); for (XmlTag item : rootTag.getSubTags()) { if (item.getAttributeValue("name") != null) { itemTags.add(item); } else { item.delete(); } } List<XmlTag> sorted = new ArrayList<>(itemTags); sorted.sort((item1, item2) -> { String externalName1 = item1.getAttributeValue("name"); String externalName2 = item2.getAttributeValue("name"); assert externalName1 != null && externalName2 != null; // null names were not added return externalName1.compareTo(externalName2); }); if (!sorted.equals(itemTags)) { for (XmlTag item : sorted) { rootTag.addAfter(item, null); item.delete(); } } }