배열을 오름차순 하고 싶으면,
Arrays.sort(arr);
배열을 내림차순 하고 싶으면,
Arrays.sort(arr, Collections.reverseOrder());
package DM20220402;
import java.util.Arrays;
import java.util.Collections;
public class Sort {
public static void main(String[] args) {
String arr[] = {"apple","orange","banana","pear","peach","melon"};
Arrays.sort(arr);
for (String i : arr) {
System.out.print("["+i+"]");
}
System.out.println();
Arrays.sort(arr, Collections.reverseOrder());
for (String i : arr) {
System.out.print("["+i+"]");
}
}
}
(결과)
[apple][banana][melon][orange][peach][pear]
[pear][peach][orange][melon][banana][apple]