1Arrays.sort(names, 0, names.length, Collections.reverseOrder());
2System.out.println("String array after sorting in descending order : " + Arrays.toString(names));
3
1import java.util.*;
2import java.lang.*;
3import java.io.*;
4import java.util.Arrays;
5
6class Ideone
7{
8 public static void main (String[] args) throws java.lang.Exception
9 {
10 final String[] data = new String[] {"Raaata", "Tatatara", "Ratatouille", "Gygyghhhygra", "Bla", "Toto"};
11 Arrays.sort(data, new Comparator<String>() {
12 public int compare(String s1, String s2) {
13 int idx1 = s1.toLowerCase().indexOf("ra");
14 int idx2 = s2.toLowerCase().indexOf("ra");
15 if(idx1 == -1) {
16 idx1 = Integer.MAX_VALUE;
17 }
18 if(idx2 == -1) {
19 idx2 = Integer.MAX_VALUE;
20 }
21 Integer i1 = new Integer(idx1);
22 Integer i2 = new Integer(idx2);
23 return i1.compareTo(i2);
24 }
25 });
26 System.out.println("Sorted array=" + Arrays.toString(data));
27 }
28}
29
1String[] names = {"John", "Steve", "Shane", "Adam", "Ben"};
2System.out.println("String array before sorting : " + Arrays.toString(names));
3Arrays.sort(names);
4System.out.println("String array after sorting in ascending order : " + Arrays.toString(names));
5
1import java.util.*;
2import java.lang.*;
3import java.io.*;
4import java.util.Arrays;
5
6class Ideone
7{
8 public static void main (String[] args) throws java.lang.Exception
9 {
10 final String[] data = new String[] {"Raaata", "Tatatara", "Ratatouille", "Gygyghhhygra",
11 "Rasatouille", "Paratouille", "Parasouille", "Bla", "Toto"};
12 Arrays.sort(data, new Comparator<String>() {
13 @Override
14 public int compare(String s1, String s2) {
15 int idx1 = s1.toLowerCase().indexOf("ra");
16 int idx2 = s2.toLowerCase().indexOf("ra");
17 if(idx1 == -1 || idx2 == -1) {
18 if(idx1 == -1) {
19 idx1 = Integer.MAX_VALUE;
20 }
21 if(idx2 == -1) {
22 idx2 = Integer.MAX_VALUE;
23 }
24 } else if(idx1 == idx2) {
25 return s1.substring(idx1).compareTo(s2.substring(idx2));
26 }
27
28 Integer i1 = new Integer(idx1);
29 Integer i2 = new Integer(idx2);
30 return i1.compareTo(i2);
31 }
32 });
33 System.out.println("Sorted array=" + Arrays.toString(data));
34 }
35}
36
1import java.util.Arrays;
2
3// creating an array with integers
4int[] array = {7, 4, 2, 1, 19};
5// this is the sorting part just one function ready to be used
6Arrays.sort(array);
7// prints [1, 2, 4, 7, 19]
8System.out.println(Arrays.toString(array));
9
1// How to Sort Object Array in Java using Comparator and Comparable
2Course[] courses = new Course[4];
3courses[0] = new Course(101, "Java", 200);
4courses[1] = new Course(201, "Ruby", 300);
5courses[2] = new Course(301, "Python", 400);
6courses[3] = new Course(401, "Scala", 500);
7
8System.out.println("Object array before sorting : " + Arrays.toString(courses));
9
10Arrays.sort(courses);
11System.out.println("Object array after sorting in natural order : " + Arrays.toString(courses));
12
13Arrays.sort(courses, new Course.PriceComparator());
14System.out.println("Object array after sorting by price : " + Arrays.toString(courses));
15
16Arrays.sort(courses, new Course.NameComparator());
17System.out.println("Object array after sorting by name : " + Arrays.toString(courses));
18
1int compare(T o1,
2 T o2)
3
4Retourne un entier négatif (<0), zéro ou un entier positif (>0) si le premier argument est plus petit, égale à ou plus grand que le deuxième argument.
5