Java - general

Array, sort (sort an array)

java.util.Arrays
An Array can be sorted with the static method java.util.Arrays.sort(..).

String strs[] = {"Birne", "Apfel", "Banane"};
Arrays.sort(strs);


With ignoring case:

String strs[] = {"Birne", "Apfel", "banane"};
Arrays.sort(strs, String.CASE_INSENSITIVE_ORDER);




With own objects:

class MyObject {
   Integer id;
   String  text;
 
   public MyObject(Integer id, String text){
      this.id = id;
      this.text = text;
   }
}
 
public void sortDemo() {
   MyObject[] myObject = {new MyObject(2, "hallo"), new MyObject(1, "hey")};
   Arrays.sort(myObject, new Comparator<MyObject>(){
      public int compare(MyObject arg0, MyObject arg1) {
         return arg0.id.compareTo(arg1.id);
      }
   });
}

Eigene Werkzeuge
Werkzeuge

gratis Counter by GOWEB
seit 9.10.2007