[Schmitzm-commits] r2140 - trunk/schmitzm-core/src/main/java/de/schmitzm/lang

scm-commit at wald.intevation.org scm-commit at wald.intevation.org
Mon Nov 12 14:33:06 CET 2012


Author: mojays
Date: 2012-11-12 14:33:06 +0100 (Mon, 12 Nov 2012)
New Revision: 2140

Modified:
   trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java
Log:
LangUtil: new method to sort a list (naive!)

Modified: trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java
===================================================================
--- trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java	2012-11-11 19:51:44 UTC (rev 2139)
+++ trunk/schmitzm-core/src/main/java/de/schmitzm/lang/LangUtil.java	2012-11-12 13:33:06 UTC (rev 2140)
@@ -2643,4 +2643,24 @@
 		return string;
 	}
 
+    /**
+     * Sorts a list according to a {@link Comparator}.
+     * @param sourceList list to sort
+     * @param comparator comparator used for sort
+     * @param destList list to store the sorted data in (if {@code null} the
+     *                 source list will be changed!)
+     */
+    public static <T> List<T> sortList(List<T> sourceList, Comparator<T> comparator, List<T> destList) {
+      if ( destList == null ) {
+        destList = sourceList;
+        destList.clear();
+      }
+      // Sort items in TreeSet
+      TreeSet<T> resultSorted = new TreeSet<T>(comparator);
+      resultSorted.addAll(sourceList);
+      // But items (now sorted) in destination list
+      destList.addAll(resultSorted);
+      
+      return destList;
+    }
 }



More information about the Schmitzm-commits mailing list