From 700199ceedd0067e49355ff388a59ed47a6da864 Mon Sep 17 00:00:00 2001 From: Andreas Haufler Date: Mon, 23 Oct 2023 17:09:17 +0200 Subject: [PATCH] Permits sorting lookup values by a given field. Fixes: SIRI-857 --- .../java/sirius/biz/codelists/LookupTable.java | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/main/java/sirius/biz/codelists/LookupTable.java b/src/main/java/sirius/biz/codelists/LookupTable.java index 09fa3e9ff..a6f571bd1 100644 --- a/src/main/java/sirius/biz/codelists/LookupTable.java +++ b/src/main/java/sirius/biz/codelists/LookupTable.java @@ -23,6 +23,7 @@ import javax.annotation.Nullable; import java.util.Collection; import java.util.Collections; +import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.Optional; @@ -60,7 +61,7 @@ public abstract class LookupTable { public static final String CONFIG_KEY_MAPPING_FIELD = "mappingsField"; private final String mappingsField; - enum CodeCase { + protected enum CodeCase { LOWER, UPPER, VERBATIM } @@ -428,6 +429,19 @@ public String forceNormalize(String code) { return normalize(code).orElseGet(() -> normalizeCodeValue(code)); } + /** + * Provides a comparator which sorts codes by the occurrence order within the list. + * + * @param field the field to sort by. This can be any numeric field. Note that the YAML loader for Jupiter can + * generate such fields when specifying a rowNumber. + * See: YAML Loader + * @return a comparator to sort codes by their occurrence order + */ + public Comparator comparator(String field) { + return Comparator.comparing(code -> fetchFieldValue(code, field).asInt(100)) + .thenComparing(this::normalizeCodeValue); + } + /** * Normalizes the given code by using the given mapping. *