trier un tableau de string java code example

Example 1: trier un tableau de string java

Arrays.sort(names, 0, names.length, Collections.reverseOrder());
System.out.println("String array after sorting in descending order : " + Arrays.toString(names));

Example 2: trier un tableau de string java

import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Arrays;
 
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        final String[] data = new String[] {"Raaata", "Tatatara", "Ratatouille", "Gygyghhhygra", "Bla", "Toto"};
        Arrays.sort(data, new Comparator<String>() {
            public int compare(String s1, String s2) {
                int idx1 = s1.toLowerCase().indexOf("ra");
                int idx2 = s2.toLowerCase().indexOf("ra");
                if(idx1 == -1) {
                    idx1 = Integer.MAX_VALUE;
                }
                if(idx2 == -1) {
                    idx2 = Integer.MAX_VALUE;
                }
                Integer i1 = new Integer(idx1);
                Integer i2 = new Integer(idx2);
                return i1.compareTo(i2);
            }
        });
        System.out.println("Sorted array=" + Arrays.toString(data));
    }
}

Example 3: trier un tableau de string java

import java.util.Arrays;

// creating an array with integers
int[] array = {7, 4, 2, 1, 19};
// this is the sorting part just one function ready to be used
Arrays.sort(array);
// prints [1, 2, 4, 7, 19]
System.out.println(Arrays.toString(array));

Example 4: trier un tableau de string java

int compare(T o1,
          T o2)
 
Retourne 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.

Example 5: trier un tableau de string java

import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Arrays;
  
class Ideone
{
    public static void main (String[] args) throws java.lang.Exception
    {
        final String[] data = new String[] {"Raaata", "Tatatara", "Ratatouille", "Gygyghhhygra", 
            "Rasatouille", "Paratouille", "Parasouille", "Bla", "Toto"};
        Arrays.sort(data, new Comparator<String>() {
            @Override
            public int compare(String s1, String s2) {
                int idx1 = s1.toLowerCase().indexOf("ra");
                int idx2 = s2.toLowerCase().indexOf("ra");
                if(idx1 == -1 || idx2 == -1) {
                    if(idx1 == -1) {
                        idx1 = Integer.MAX_VALUE;
                    }
                    if(idx2 == -1) {
                        idx2 = Integer.MAX_VALUE;
                    }
                } else if(idx1 == idx2) {
                    return s1.substring(idx1).compareTo(s2.substring(idx2));
                }
                 
                Integer i1 = new Integer(idx1);
                Integer i2 = new Integer(idx2);
                return i1.compareTo(i2);
            }
        });
        System.out.println("Sorted array=" + Arrays.toString(data));
    }
}

Example 6: trier un tableau de string java

// How to Sort Object Array in Java using Comparator and Comparable
Course[] courses = new Course[4];
courses[0] = new Course(101, "Java", 200);
courses[1] = new Course(201, "Ruby", 300);
courses[2] = new Course(301, "Python", 400);
courses[3] = new Course(401, "Scala", 500);
       
System.out.println("Object array before sorting : " + Arrays.toString(courses));
       
Arrays.sort(courses);
System.out.println("Object array after sorting in natural order : " + Arrays.toString(courses));
       
Arrays.sort(courses, new Course.PriceComparator());
System.out.println("Object array after sorting by price : " + Arrays.toString(courses));
       
Arrays.sort(courses, new Course.NameComparator());
System.out.println("Object array after sorting by name : " + Arrays.toString(courses));

Example 7: trier un tableau de string java

String[] names = {"John", "Steve", "Shane", "Adam", "Ben"};
System.out.println("String array before sorting : " + Arrays.toString(names));
Arrays.sort(names); 
System.out.println("String array after sorting in ascending order : " + Arrays.toString(names));

Tags:

Misc Example