@Override
    public List<Element> sortHistory(final Collection<HistoryRecord> records, final Document doc) {
        LOG.fine("Sotring messages by UUID.");

        // group Nodes by UUID        
        final SortedMap<UUID, List<HistoryRecord>> idGroups = new TreeMap<UUID, List<HistoryRecord>>();
        List<HistoryRecord> list;
        Element element;
        NodeList nodeList;
        Node uuidNode;
        UUID uuid;
        for (HistoryRecord r : records) {
            element = r.getData();
            nodeList = element.getElementsByTagName(FIELD_NAME_UUID);
            if (nodeList.getLength() > 0) {
                uuidNode = nodeList.item(0);
                uuid = UUID.fromString(uuidNode.getFirstChild().getTextContent());
                list = idGroups.get(uuid);
                if (list == null) {
                    list = new ArrayList<HistoryRecord>();
                    idGroups.put(uuid, list);
                }
                list.add(r);
            }
        }

        final List<Element> result = new ArrayList<Element>(records.size());
        Element group;
        for (Entry<UUID, List<HistoryRecord>> e : idGroups.entrySet()) {
            group = doc.createElement("Group");
            group.setAttribute("UUID", e.getKey().toString());
            
            for (HistoryRecord r : e.getValue()) {
                group.appendChild(convertRecordToXML(r, doc));
            }

            result.add(group);
        }

        return result;
    }