2828import java .util .Objects ;
2929import java .util .Optional ;
3030import java .util .Set ;
31+ import java .util .stream .Collectors ;
32+ import java .util .stream .StreamSupport ;
3133
3234import org .slf4j .Logger ;
3335import org .slf4j .LoggerFactory ;
@@ -268,14 +270,17 @@ public ObjectNode docToJsonNode(String documentUri) throws InvalidSPDXAnalysisEx
268270 doc .put (SpdxConstantsCompatV2 .PROP_DOCUMENT_NAMESPACE .getName (), documentUri );
269271 ArrayNode packages = getDocElements (documentUri , SpdxConstantsCompatV2 .CLASS_SPDX_PACKAGE , relationships );
270272 if (!packages .isEmpty ()) {
273+ sortArrayNode (packages );
271274 doc .set (SpdxConstantsCompatV2 .PROP_DOCUMENT_PACKAGES .getName (), packages );
272275 }
273276 ArrayNode files = getDocElements (documentUri , SpdxConstantsCompatV2 .CLASS_SPDX_FILE , relationships );
274277 if (!files .isEmpty ()) {
278+ sortArrayNode (files );
275279 doc .set (SpdxConstantsCompatV2 .PROP_DOCUMENT_FILES .getName (), files );
276280 }
277281 ArrayNode snippets = getDocElements (documentUri , SpdxConstantsCompatV2 .CLASS_SPDX_SNIPPET , relationships );
278282 if (!snippets .isEmpty ()) {
283+ sortArrayNode (snippets );
279284 doc .set (SpdxConstantsCompatV2 .PROP_DOCUMENT_SNIPPETS .getName (), snippets );
280285 }
281286 //Remove duplicate relationships
@@ -302,7 +307,7 @@ public ObjectNode docToJsonNode(String documentUri) throws InvalidSPDXAnalysisEx
302307 relatedIds .add (relatedID );
303308 }
304309 }
305-
310+ sortArrayNode ( deDupedRelationships );
306311
307312 doc .set (SpdxConstantsCompatV2 .PROP_DOCUMENT_RELATIONSHIPS .getName (), deDupedRelationships );
308313 ObjectNode output ;
@@ -326,6 +331,16 @@ public ObjectNode docToJsonNode(String documentUri) throws InvalidSPDXAnalysisEx
326331 }
327332 }
328333
334+ /**
335+ * Sorts the elements of an ArrayNode in place
336+ * @param an ArrayNode to sort
337+ */
338+ private void sortArrayNode (ArrayNode an ) {
339+ List <JsonNode > arrayElements = StreamSupport .stream (an .spliterator (), false ).sorted (NODE_COMPARATOR ).collect (Collectors .toList ());
340+ an .removeAll ();
341+ an .addAll (arrayElements );
342+ }
343+
329344 /**
330345 * Convert a typed value into an ObjectNode adding all stored properties
331346 * @param documentUri Document namespace or Uri
0 commit comments