Java Comparator and don't forget about the feature of mergeSort!
How to keep order in Comparator? return 0;\nSome ppl just return 0; and may forget to think about why return 0 can keep the original order.\n\n---\nReason:\nBec
How to keep order in Comparator? `return 0;`\nSome ppl just `return 0;` and may forget to think about why `return 0` can keep the original order.\n\n---\nReason:\nBecause when we use `Comparator` for `Arrays.sort()`, the implementation is **mergeSort**, you can check official document for [Arrays.sort(T[] a, Comparator<? super T> c)](https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#sort(T[],%20java.util.Comparator)). And **merge sort will keep the order if 2 elements are the same!!!**\n\nHowever, for `Array.sort()` methods like `Array.sort(int[]/double[]/char[])` without using `Comparator`, the implementation is **quickSort**, it will not keep the original order. For example, check the Java doc for sorting `int[]` array [public static void sort(int[] a)](https://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html#sort(int[])). \nHowever, thanks for @GregorGk mentioning, these are all primitive types array, no need to consider their order.  \n\n---\nFinal code:\n\n```java\nclass Solution {\n    public String[] reorderLogFiles(String[] logs) {\n        if(logs == null || logs.length == 0) return null;\n        \n        Arrays.sort(logs, new MyComparator());\n        return logs;        \n    }\n    \n    public class MyComparator implements Comparator<String>{\n        @Override\n        public int compare(String log1, String log2){\n            int idx1 = log1.indexOf(" ") + 1;\n            int idx2 = log2.indexOf(" ") + 1;\n            if(log1.charAt(idx1) >= \'a\' && log1.charAt(idx1) <= \'z\' && \n               log2.charAt(idx2) >= \'a\' && log2.charAt(idx2) <= \'z\'){\n                String tail1 = log1.substring(idx1);\n                String tail2 = log2.substring(idx2);\n                int temp = tail1.compareTo(tail2);\n                if(temp != 0) return temp;\n                else return log1.compareTo(log2);\n            }else if(log1.charAt(idx1) >= \'a\' && log1.charAt(idx1) <= \'z\') return -1;\n            else if(log2.charAt(idx2) >= \'a\' && log2.charAt(idx2) <= \'z\') return 1;\n            else return 0; // if both digit-logs, then keep the order\n        }\n    }\n}\n```
[]