Arraylist in verschiedene Kategorien sortieren

D

DeNiXP

Neues Mitglied
0
Hallo,

ich hab ein Problem mit der Sortierung von einer Arrayliste.

Ich habe eine XML Datei die ich geparst habe und mir den Titel, das Datum und die Beschreibung in ein Textview auslesen lasse.

Das Sortieren nach dem Titel funktioniert problemlos, jedoch möchte ich jetzt gern die Arraylist nach Datum und der Bescheibung sortieren.

Kann mir jemand helfen ? Wie kann ich die Daten so sortieren, dass der Aufbau bei der Anzeige, die selbe bleibt ?

Titel
Bescheibung (Datum)

Hier ist mein bisheriger Code..

Code:
[SIZE=2][COLOR=#7f0055][B][SIZE=2][COLOR=#7f0055]private[/COLOR][/SIZE][/B]
[LEFT][/COLOR][/SIZE][SIZE=2]List<RSSItem> [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]items[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];[/SIZE]

[SIZE=2][COLOR=#646464][SIZE=2][COLOR=#646464]@Override[/COLOR][/SIZE][/COLOR][/SIZE]
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]public[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] onCreate(Bundle savedInstanceState) {[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]super[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2].onCreate(savedInstanceState);[/SIZE]
[SIZE=2]setContentView(R.layout.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]main[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]);[/SIZE]
[SIZE=2]loadFeed();[/SIZE][/LEFT]
[/LEFT]

 

[LEFT][SIZE=2]} [/SIZE]
[LEFT][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// XML [U]Parsen[/U][/COLOR][/SIZE][/COLOR][/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]private[/COLOR][/SIZE][/COLOR][/SIZE][/B][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] loadFeed() {[/SIZE][/LEFT]
[/LEFT]

 

[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]try[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] {[/SIZE]
[LEFT][SIZE=2]FeedParser parser = [/SIZE][/LEFT]
[/LEFT]

 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] FeedParser([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]this[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2].getResources().getXml(R.xml.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]badeseenliste[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2]));[/SIZE]

[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]long[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] start = System.[I]currentTimeMillis[/I]();[/SIZE]
[LEFT][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]items[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = parser.parse();[/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]long[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] duration = System.[I]currentTimeMillis[/I]() - start;[/SIZE]
[SIZE=2]Log.[I]i[/I]([/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"SeenAtlas"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Parser duration="[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + duration);[/SIZE]
[SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]//pubDate[/COLOR][/SIZE][/COLOR][/SIZE]
[B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]final[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] List<String> titles = [/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ArrayList<String>([/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]items[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2].size());[/SIZE][/LEFT]
[/LEFT]

 

[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]for[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]final[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] RSSItem item : [/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]items[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]) {[/SIZE]
[LEFT][SIZE=2]titles.add(item.getTitle() + [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"\n"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + item.getDescription() + [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]" \t ("[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] + item.getPubDate()+ [/SIZE][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]")"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);[/SIZE][/LEFT]
[/LEFT]

 

[LEFT][SIZE=2]Collections.[I]sort[/I](titles);[/SIZE][/LEFT]

 

[LEFT][SIZE=2]}[/SIZE][/LEFT]

 
[LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]this[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2].setListAdapter([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]new[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ArrayAdapter<String>([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]this[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2], R.layout.[/SIZE][I][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]row[/COLOR][/SIZE][/COLOR][/SIZE][/I][SIZE=2], titles));[/SIZE]
[SIZE=2]}[/SIZE][/LEFT]

Danke im vorraus :)
 
Ich verstehe dein "Array umsortieren ohne die Anzeige zu ändern"-Teil nicht so ganz.
Soll sich die Reihenfolge der Items nun ändern oder nicht?

Eine Möglichkeit wäre Collections.sort(List,Comparator) mit einem geeigneten Comparator für deine Strings.

Dort müsstest du dann das Datum aus dem String wieder heraus parsen.

Eleganter wäre deshalb, wenn du direkt "items" und nicht nur die Stringabklatsche nutztest.
Dann kannst du in dem Comparator einfach an das gewünschte Feld delegieren.
 
Hallo, danke für Ihre schnelle Antwort

Ich möchte die Arrayliste normal sortieren:

1. nach Titel
2. nach Datum
3. nach Beschreibung

Jeder Datensatz in der Arrayliste verfügt über ein Titel, Beschreibung und Datum.

der Aufbau wie die Daten aber angezeigt werden sollen, soll weiterhin wie folgt aussehn:
Titel1
Beschreibung1 (Datum1)
-----------------------
Titel2
Beschreibung2 (Datum2)
-----------------------
Titel3
Beschreibung3 (Datum3)
 
new Comparator<deinObjekt>{
public int compare(deinObjekt1, deinObjekt2){
int result = deinObjekt1.titel.compareTo(deinObjekt2.titel);
if(result!=0)return result;
result = deinObjekt1.datum.compareTo(deinObjekt2.datum);
...
}
}
Datum kannste auch schneller als int vergleichen
 

Ähnliche Themen

B
Antworten
3
Aufrufe
1.309
swa00
swa00
D
Antworten
23
Aufrufe
2.561
Data2006
D
OnkelLon
Antworten
13
Aufrufe
1.987
OnkelLon
OnkelLon
Zurück
Oben Unten