File [] files = new file (DIR). listfiles ();
Arrays. Sort (files); when using it, pay attention to the description of sort. The comparable interface must be implemented for the comparison type.
Java array sorting arrays. sort, and the usage of the comparator interface sometimes need to sort the elements in the logarithm group. Of course, you can write a proper sorting method by yourself. But since there is an arrays. Sort sorting method in the Java package, why not use it when there are few array elements? . F 'K, M8 s J1 F k']/Z
Sorting an array 1. Number sorting int [] intarray = new int [] {4, 1, 3,-23 }; K, '"_ # X7 m
Arrays. Sort (intarray ); ${3 K) O % E1 W5 T7 X % t' I
Output: [-23, 1, 3, 4]
(B3 B3 t)} "B + I $ x 2. Sort strings in upper case and lower case string [] strarray = new string [] {"Z", "A", "C "};
2 W5 g * a "p! ': O2 o Arrays. Sort (strarray );
9 y & D &} "W" K8 C Output: [C, A, Z] 6 q (W2] 5 p I0 K & C * Q6 R}
3. Strictly sort by alphabet, that is, ignore case-insensitive sort
. F "| 8 U1 e M9 R Arrays. Sort (strarray, String. case_insensitive_order ); 8 R7 I5 D0 ["[2 U2 Q2 K
Output: [A, C, Z]
9 h k "P5 L6 s 4. Reverse sorting, reverse-Order sort
7 _ (^,} 9 x + T + B Arrays. Sort (strarray, collections. reverseorder ());
/R0 {$ z * o8 y-O7 @ Output: [Z, a, c]
. C, C "{" l % F) x 5. Ignore case-insensitive reverse sorting case-insensitive reverse-Order sort
5 F8 [0 N0 M-]. c ++ ~; M8 Z3 J $ G Arrays. Sort (strarray, String. case_insensitive_order ); &] #]/Z) N * K. D) I0 F
Collections. Reverse (arrays. aslist (strarray )); # M3 S. i7 m} 0 \ % A9 n % H % L; R
Output: [Z, C, A]E8 T4 O + z x: H )}
The most common mistake for Java beginners is to try to write some methods to complete the array sorting function. In fact, the array sorting function has already been implemented in Java APIs, we do not have to repeat the wheel. (X! [+ D1? & G
The arrays class has a static method sort. With this method, we can import the array we want to sort into the sorting. Because we pass in an array reference, the sorting result is also
This reference is used to change the array. for sorting integers and strings, JDK provides the default implementation. If you want to sort an array of objects, you must implement it by yourself.
Java. util. comparator interface.