I have a ListView that is populated by three (3) ArrayList‘s:
items, ratings and comments
However, I need to sort items by ignoring the leading ‘the’. I already have accomplished this by rearranging the items ArrayList using a Collections.sort (see code below), but this is the issue: the comments and ratings are not rearranged so it comes out of order in the ListView.
For example, if the list was:
- Cars 3 4
- People 5 3
- The Animals 7 4
After items sort I am getting:
- The Animals 3 4
- Cars 5 3
- People 7 4
So the items are lining up as I’d like but the associated comments and ratings aren’t sorted. I am not sure how to make that happen and where to place it. I think in the ArrayAdapter?
Here is the code I do to change the items list:
Comparator<String> ignoreLeadingThe = new Comparator<String>() {
public int compare(String a, String b) {
a = a.replaceAll("(?i)^the\\s+", "");
b = b.replaceAll("(?i)^the\\s+", "");
return a.compareToIgnoreCase(b);
}
};
Collections.sort(items, ignoreLeadingThe);
Here is the questions? Where and how can I sort the ratings and comments lists based on the position of the item list?
Edit:
this is my getView code in my ArrayAdapter:
ItemObject io = getItem(position);
String name = io.name;
String total = io.total;
String rating = io.ratings;
String comment = io.comments;
holder.t1.setText(name);
holder.t2.setText(total);
holder.t3.setText(comment);
holder.t4.setText(rating);
Note: There is a 4th ArrayList called total I did not mention in the above example.
You should look at creating a class to wrap your items in the ArrayList, something like this:
Then have an ArrayList of these objects instead:
Then in your comparator, just do it like you’re doing, but testing against
MyItem.iteminstead of justaandb. Something like this: