Unterschiede
Hier werden die Unterschiede zwischen der gewählten und der aktuellen Version gezeigt.
java:array_sortieren 2007/10/08 09:49 | java:array_sortieren 2020/01/22 20:59 aktuell | ||
---|---|---|---|
Zeile 1: | Zeile 1: | ||
- | ====== Array sortieren ====== | + | [[java|Java - Allgemein]]\\ |
- | Ein Array kann einfach durch die statische Methode Arrays.sort(..) sortiert werden. | + | ====== Array sortieren (Sort an array) ====== |
+ | //java.util.Arrays//\\ | ||
+ | Ein Array kann einfach durch die statische Methode java.util.Arrays.sort(..) sortiert werden. | ||
<code java> | <code java> | ||
- | String strs[] = {"Birne", "Apfel", "Banane"} | + | String strs[] = {"Birne", "Apfel", "Banane"}; |
Arrays.sort(strs); | Arrays.sort(strs); | ||
+ | </code> | ||
+ | \\ | ||
+ | Das Ganze funktioniert auch unter Ignorierung der Groß-/Kleinschreibung: | ||
+ | <code java> | ||
+ | String strs[] = {"Birne", "Apfel", "banane"}; | ||
+ | Arrays.sort(strs, String.CASE_INSENSITIVE_ORDER); | ||
+ | </code> | ||
+ | \\ | ||
+ | <html> | ||
+ | <script type="text/javascript"><!-- | ||
+ | google_ad_client="pub-9681858985507948"; | ||
+ | google_ad_width = 468; | ||
+ | google_ad_height = 60; | ||
+ | google_ad_format = "468x60_as"; | ||
+ | google_ad_type = "text"; | ||
+ | google_ad_channel = ""; | ||
+ | google_color_border = "cccccc"; | ||
+ | google_color_bg = "FFFFFF"; | ||
+ | google_color_link = "1d2d8c"; | ||
+ | google_color_text = "000000"; | ||
+ | google_color_url = "1d2d8c"; | ||
+ | //--> | ||
+ | </script> | ||
+ | <script type="text/javascript" | ||
+ | src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> | ||
+ | </script> | ||
+ | </html> | ||
+ | \\ | ||
+ | \\ | ||
+ | Und natürlich auch mit eigenen Objekten: | ||
+ | <code java> | ||
+ | class MyObject { | ||
+ | public MyObject(Integer id, String irgendwas){ | ||
+ | this.id = id; | ||
+ | this.irgendwas = irgendwas; | ||
+ | } | ||
+ | Integer id; | ||
+ | String irgendwas; | ||
+ | } | ||
+ | |||
+ | public void sortDemo() { | ||
+ | MyObject[] myObject = {new MyObject(2, "hallo"), new MyObject(1, "huhu")}; | ||
+ | Arrays.sort(myObject, new Comparator<MyObject>(){ | ||
+ | public int compare(MyObject arg0, MyObject arg1) { | ||
+ | return arg0.id.compareTo(arg1.id); | ||
+ | } | ||
+ | }); | ||
+ | } | ||
</code> | </code> |