]> gerrit.simantics Code Review - simantics/district.git/commitdiff
Moved compareNatural to TechTypeUtils 85/4485/1
authorReino Ruusu <reino.ruusu@semantum.fi>
Fri, 2 Oct 2020 05:47:21 +0000 (08:47 +0300)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Tue, 6 Oct 2020 21:43:06 +0000 (21:43 +0000)
gitlab #93

Change-Id: I7450918d7580e92519d92e3a6e53dce9986667fb
(cherry picked from commit 57283335590a05798c49460fd63da079e9795079)

org.simantics.district.network/src/org/simantics/district/network/techtype/TechTypeUtils.java
org.simantics.district.network/src/org/simantics/district/network/techtype/variable/Functions.java

index dce4abb7e47f904ba21e5e1a50b2acf7eaf925bb..377253dde3d108b433c572d6e0ddc576872b30ab 100644 (file)
@@ -346,4 +346,46 @@ public class TechTypeUtils {
                if (name == null) name = "_" + DEFAULT_KEY_NAME;
                return name; 
        }
+
+       /**
+        * Compare strings so that contained numbers are sorted in numerical order instead of lexicographic.
+        * (From https://stackoverflow.com/questions/104599/sort-on-a-string-that-may-contain-a-number)
+        */
+       public static final int compareNatural(String s1, String s2) {
+               // Skip all identical characters
+               int len1 = s1.length();
+               int len2 = s2.length();
+               int i;
+               char c1, c2;
+               for (i = 0, c1 = 0, c2 = 0; (i < len1) && (i < len2) && (c1 = s1.charAt(i)) == (c2 = s2.charAt(i)); i++)
+                       ;
+
+               // Check end of string
+               if (c1 == c2)
+                       return (len1 - len2);
+
+               // Check digit in first string
+               if (Character.isDigit(c1)) {
+                       // Check digit only in first string
+                       if (!Character.isDigit(c2))
+                               return (1);
+
+                       // Scan all integer digits
+                       int x1, x2;
+                       for (x1 = i + 1; (x1 < len1) && Character.isDigit(s1.charAt(x1)); x1++)
+                               ;
+                       for (x2 = i + 1; (x2 < len2) && Character.isDigit(s2.charAt(x2)); x2++)
+                               ;
+
+                       // Longer integer wins, first digit otherwise
+                       return (x2 == x1 ? c1 - c2 : x1 - x2);
+               }
+
+               // Check digit only in second string
+               if (Character.isDigit(c2))
+                       return (-1);
+
+               // No digits
+               return (c1 - c2);
+       }
 }
index e9065f4999d40be367014c198bd820a9160b6f1b..a6a3ed2d91fad6d38c49b6fd280e3b9249b45b98 100644 (file)
@@ -89,47 +89,8 @@ public class Functions {
                ArrayList<String> result = new ArrayList<String>(data.keySet());
                
                // Sort so that all numbers are in growing order
-               result.sort(Functions::compareNatural);
+               result.sort(TechTypeUtils::compareNatural);
                
                return result;
        }
-       
-       // From https://stackoverflow.com/questions/104599/sort-on-a-string-that-may-contain-a-number
-       private static final int compareNatural(String s1, String s2) {
-               // Skip all identical characters
-               int len1 = s1.length();
-               int len2 = s2.length();
-               int i;
-               char c1, c2;
-               for (i = 0, c1 = 0, c2 = 0; (i < len1) && (i < len2) && (c1 = s1.charAt(i)) == (c2 = s2.charAt(i)); i++)
-                       ;
-
-               // Check end of string
-               if (c1 == c2)
-                       return (len1 - len2);
-
-               // Check digit in first string
-               if (Character.isDigit(c1)) {
-                       // Check digit only in first string
-                       if (!Character.isDigit(c2))
-                               return (1);
-
-                       // Scan all integer digits
-                       int x1, x2;
-                       for (x1 = i + 1; (x1 < len1) && Character.isDigit(s1.charAt(x1)); x1++)
-                               ;
-                       for (x2 = i + 1; (x2 < len2) && Character.isDigit(s2.charAt(x2)); x2++)
-                               ;
-
-                       // Longer integer wins, first digit otherwise
-                       return (x2 == x1 ? c1 - c2 : x1 - x2);
-               }
-
-               // Check digit only in second string
-               if (Character.isDigit(c2))
-                       return (-1);
-
-               // No digits
-               return (c1 - c2);
-       }
 }