Added forgotten Comparator
authortobiasKaminsky <tobias@kaminsky.me>
Sun, 14 Sep 2014 14:05:34 +0000 (16:05 +0200)
committertobiasKaminsky <tobias@kaminsky.me>
Sun, 14 Sep 2014 14:05:34 +0000 (16:05 +0200)
src/com/owncloud/android/datamodel/AlphanumComparator.java [new file with mode: 0644]

diff --git a/src/com/owncloud/android/datamodel/AlphanumComparator.java b/src/com/owncloud/android/datamodel/AlphanumComparator.java
new file mode 100644 (file)
index 0000000..f78ef25
--- /dev/null
@@ -0,0 +1,127 @@
+/*\r
+ * The Alphanum Algorithm is an improved sorting algorithm for strings\r
+ * containing numbers.  Instead of sorting numbers in ASCII order like\r
+ * a standard sort, this algorithm sorts numbers in numeric order.\r
+ *\r
+ * The Alphanum Algorithm is discussed at http://www.DaveKoelle.com\r
+ *\r
+ *\r
+ * This library is free software; you can redistribute it and/or\r
+ * modify it under the terms of the GNU Lesser General Public\r
+ * License as published by the Free Software Foundation; either\r
+ * version 2.1 of the License, or any later version.\r
+ *\r
+ * This library is distributed in the hope that it will be useful,\r
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of\r
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU\r
+ * Lesser General Public License for more details.\r
+ *\r
+ * You should have received a copy of the GNU Lesser General Public\r
+ * License along with this library; if not, write to the Free Software\r
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA\r
+ *\r
+ */\r
+\r
+package com.owncloud.android.datamodel;\r
+import java.util.Comparator;\r
+\r
+/**\r
+ * This is an updated version with enhancements made by Daniel Migowski,\r
+ * Andre Bogus, and David Koelle\r
+ *\r
+ * To convert to use Templates (Java 1.5+):\r
+ *   - Change "implements Comparator" to "implements Comparator<String>"\r
+ *   - Change "compare(Object o1, Object o2)" to "compare(String s1, String s2)"\r
+ *   - Remove the type checking and casting in compare().\r
+ *\r
+ * To use this class:\r
+ *   Use the static "sort" method from the java.util.Collections class:\r
+ *   Collections.sort(your list, new AlphanumComparator());\r
+ */\r
+public class AlphanumComparator implements Comparator<OCFile>\r
+{\r
+    private final boolean isDigit(char ch)\r
+    {\r
+        return ch >= 48 && ch <= 57;\r
+    }\r
+\r
+    /** Length of string is passed in for improved efficiency (only need to calculate it once) **/\r
+    private final String getChunk(String s, int slength, int marker)\r
+    {\r
+        StringBuilder chunk = new StringBuilder();\r
+        char c = s.charAt(marker);\r
+        chunk.append(c);\r
+        marker++;\r
+        if (isDigit(c))\r
+        {\r
+            while (marker < slength)\r
+            {\r
+                c = s.charAt(marker);\r
+                if (!isDigit(c))\r
+                    break;\r
+                chunk.append(c);\r
+                marker++;\r
+            }\r
+        } else\r
+        {\r
+            while (marker < slength)\r
+            {\r
+                c = s.charAt(marker);\r
+                if (isDigit(c))\r
+                    break;\r
+                chunk.append(c);\r
+                marker++;\r
+            }\r
+        }\r
+        return chunk.toString();\r
+    }\r
+\r
+    public int compare(OCFile o1, OCFile o2)\r
+    {\r
+        String s1 = (String)o1.getRemotePath().toLowerCase();\r
+        String s2 = (String)o2.getRemotePath().toLowerCase();\r
+\r
+        int thisMarker = 0;\r
+        int thatMarker = 0;\r
+        int s1Length = s1.length();\r
+        int s2Length = s2.length();\r
+\r
+        while (thisMarker < s1Length && thatMarker < s2Length)\r
+        {\r
+            String thisChunk = getChunk(s1, s1Length, thisMarker);\r
+            thisMarker += thisChunk.length();\r
+\r
+            String thatChunk = getChunk(s2, s2Length, thatMarker);\r
+            thatMarker += thatChunk.length();\r
+\r
+            // If both chunks contain numeric characters, sort them numerically\r
+            int result = 0;\r
+            if (isDigit(thisChunk.charAt(0)) && isDigit(thatChunk.charAt(0)))\r
+            {\r
+                // Simple chunk comparison by length.\r
+                int thisChunkLength = thisChunk.length();\r
+                result = thisChunkLength - thatChunk.length();\r
+                // If equal, the first different number counts\r
+                if (result == 0)\r
+                {\r
+                    for (int i = 0; i < thisChunkLength; i++)\r
+                    {\r
+                        result = thisChunk.charAt(i) - thatChunk.charAt(i);\r
+                        if (result != 0)\r
+                        {\r
+                            return result;\r
+                        }\r
+                    }\r
+                }\r
+            } else\r
+            {\r
+                result = thisChunk.compareTo(thatChunk);\r
+            }\r
+\r
+            if (result != 0)\r
+                return result;\r
+        }\r
+\r
+        return s1Length - s2Length;\r
+    }\r
+}\r