458 votes

Comment trier une liste/un tableau ?

J'ai une liste de doubles en java et je veux trier ArrayList en ordre décroissant.

L'entrée ArrayList est comme ci-dessous :

List<Double> testList = new ArrayList();

testList.add(0.5);
testList.add(0.2);
testList.add(0.9);
testList.add(0.1);
testList.add(0.1);
testList.add(0.1);
testList.add(0.54);
testList.add(0.71);
testList.add(0.71);
testList.add(0.71);
testList.add(0.92);
testList.add(0.12);
testList.add(0.65);
testList.add(0.34);
testList.add(0.62);

Le résultat devrait être le suivant

0.92
0.9
0.71
0.71
0.71
0.65
0.62
0.54
0.5
0.34
0.2
0.12
0.1
0.1
0.1

2 votes

TestList.sort(Comparator.reverseOrder()) ;

3voto

Ibrahima Timera Points 321

Par exemple, j'ai une classe Person : String name, int age ==>Constructeur new Person(name,age)

import java.util.Collections;
import java.util.ArrayList;
import java.util.Arrays;

public void main(String[] args){
    Person ibrahima=new Person("Timera",40);
    Person toto=new Person("Toto",35);
    Person alex=new Person("Alex",50);
    ArrayList<Person> myList=new ArrayList<Person>
    Collections.sort(myList, new Comparator<Person>() {
        @Override
        public int compare(Person p1, Person p2) {
            // return p1.age+"".compareTo(p2.age+""); //sort by age
            return p1.name.compareTo(p2.name); // if you want to short by name
        }
    });
    System.out.println(myList.toString());
    //[Person [name=Alex, age=50], Person [name=Timera, age=40], Person [name=Toto, age=35]]
    Collections.reverse(myList);
    System.out.println(myList.toString());
    //[Person [name=Toto, age=35], Person [name=Timera, age=40], Person [name=Alex, age=50]]

}

3voto

Afsar Ali Points 50

Si vous devez trier un objet en fonction de son identifiant dans une liste de tableaux, utilisez le flux java8.

 List<Person> personList = new ArrayList<>();

    List<Person> personListSorted =
                personList.stream()
                  .sorted(Comparator.comparing(Person::getPersonId))
                  .collect(Collectors.toList());

2voto

Donald Raab Points 742

Avec Collections Eclipse vous pourriez créer une liste double primitive, la trier puis l'inverser pour la mettre dans l'ordre décroissant. Cette approche permettrait d'éviter de mettre les doubles en boîte.

MutableDoubleList doubleList =
    DoubleLists.mutable.with(
        0.5, 0.2, 0.9, 0.1, 0.1, 0.1, 0.54, 0.71,
        0.71, 0.71, 0.92, 0.12, 0.65, 0.34, 0.62)
        .sortThis().reverseThis();
doubleList.each(System.out::println);

Si vous voulez un List<Double> alors la formule suivante fonctionnerait.

List<Double> objectList =
    Lists.mutable.with(
        0.5, 0.2, 0.9, 0.1, 0.1, 0.1, 0.54, 0.71,
        0.71, 0.71, 0.92, 0.12, 0.65, 0.34, 0.62)
        .sortThis(Collections.reverseOrder());
objectList.forEach(System.out::println);

Si vous voulez garder le type comme ArrayList<Double> vous pouvez initialiser et trier la liste à l'aide de la fonction ArrayListIterate classe d'utilité comme suit :

ArrayList<Double> arrayList =
    ArrayListIterate.sortThis(
            new ArrayList<>(objectList), Collections.reverseOrder());
arrayList.forEach(System.out::println);

Note : Je suis un participant au projet Collections Eclipse .

0voto

Ivan Kovtun Points 197

La ligne suivante devrait faire l'affaire

testList.sort(Collections.reverseOrder());

0voto

Caner Yılmaz Points 57
  yearList = arrayListOf()
    for (year in 1950 until 2021) {
        yearList.add(year)
    }

   yearList.reverse()
    val list: ArrayList<String> = arrayListOf()

    for (year in yearList) {
        list.add(year.toString())
    }

Prograide.com

Prograide est une communauté de développeurs qui cherche à élargir la connaissance de la programmation au-delà de l'anglais.
Pour cela nous avons les plus grands doutes résolus en français et vous pouvez aussi poser vos propres questions ou résoudre celles des autres.

Powered by:

X