java.util.List sortieren?

Romsl

Erfahrenes Mitglied
Hi,

ich möchte gerne eine List sortieren, finde aber leider kein Framework oder sonstige Klassen.
Falls es noch nichts implementiertes (was ich allerdings kaum glauben kann) gibt, sollte ich welches Sortierverfahren (ca. 500 Elemente, die Liste kann vorsortiert sein - muss aber nicht) verwenden?

Gruß

Romsl
 
Schau Dir mal diese Klasse an:

http://java.sun.com/j2se/1.4.2/docs/api/java/util/Collections.html#sort(java.util.List,%20java.util.Comparator)

Damit kannst Du dann Deine Liste sortieren, vorausgesetzt der Comparator tut das richtige.
 
Danke dir,

jetzt hab ich noch ein Problem. Ich habe einen eigenen Comparator der 2 Strings miteinander vergleicht. Leider kommt das Ö nach dem Z.
Was kann ich machen um die gebräuchliche Reihenfolge herzustellen?
 
Hallo!

hier mal ein paar Beispiele zur Sortierung in Java:
Java:
package de.tutorials;

import java.text.Collator;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

public class ListSortExample {
  public static void main(String[] args) {
    List<Integer> originalInts = Arrays.asList(4, 5, 2, 6, 1, 3);
    System.out.println("originalInts: " + originalInts);

    List<Integer> ascInts = new ArrayList<Integer>(originalInts);
    Collections.sort(ascInts); // sortiert die ascInts Liste direkt unter der Verwendung der
                               // Implementierung des Comparable-Interfaces (Integer implements
                               // Comparable<Integer>) aufsteigend
    System.out.println("ascInts: " + ascInts);

    List<Integer> customAscInts = new ArrayList<Integer>(originalInts);
    Collections.sort(customAscInts, newAscIntComparator());
    System.out.println("customAscInts: " + customAscInts);

    List<Integer> customDescInts = new ArrayList<Integer>(originalInts);
    Collections.sort(customDescInts, newDescIntComparator());
    System.out.println("customDescInts: " + customDescInts);

    List<String> originalStrings = Arrays.asList("XXX", "yyy", "ö", "A");
    
    List<String> ascSortedStrings = new ArrayList<String>(originalStrings);
    Collections.sort(ascSortedStrings);
    System.out.println("ascSortedStrings: " + ascSortedStrings);
    
    List<String> descSortedStrings = new ArrayList<String>(originalStrings);
    Collections.sort(descSortedStrings, new ReverseComparator<String>(String.CASE_INSENSITIVE_ORDER));
    System.out.println("descSortedStrings: " + descSortedStrings);
    
    //eine Locale (Sprach) abhängige Sortierung von Strings kann man mit dem Collator erzeugen:
    
    List<String> ascLocaleSortedStrings = new ArrayList<String>(originalStrings);
    Collections.sort(ascLocaleSortedStrings,Collator.getInstance());
    System.out.println("ascLocaleSortedStrings: " + ascLocaleSortedStrings);
    
    List<String> descLocaleSortedStrings = new ArrayList<String>(originalStrings);
    Collections.sort(descLocaleSortedStrings, new ReverseComparator<String>((Comparator<String>)(Object)Collator.getInstance()));
    System.out.println("descLocaleSortedStrings: " + descLocaleSortedStrings);
  }


  protected static Comparator<Integer> newAscIntComparator() {
    return new Comparator<Integer>() {
      @Override
      public int compare(Integer first, Integer second) {
        return Integer.valueOf(first).compareTo(Integer.valueOf(second));
      }
    };
  }
  
  public static class ReverseComparator<T> implements Comparator<T> {

    private final Comparator<T> comparator;


    public ReverseComparator(Comparator<T> comparator) {
      this.comparator = comparator;
    }


    public int compare(T first, T second) {
      return -this.comparator.compare(first, second);
    }
  }


  protected static Comparator<Integer> newDescIntComparator() {
    return new ReverseComparator<Integer>(newAscIntComparator());
  }
}

Ausgabe:
Code:
originalInts: [4, 5, 2, 6, 1, 3]
ascInts: [1, 2, 3, 4, 5, 6]
customAscInts: [1, 2, 3, 4, 5, 6]
customDescInts: [6, 5, 4, 3, 2, 1]
ascSortedStrings: [A, XXX, yyy, ö]
descSortedStrings: [ö, yyy, XXX, A]
ascLocaleSortedStrings: [A, ö, XXX, yyy]
descLocaleSortedStrings: [yyy, XXX, ö, A]

Gruß Tom
 
Was würd ich nur ohne Euch tun.

Danke du hast soeben min 1 Jahr meines Lebens gerettet.
 
Zurück