How to sort ArrayList<String> containing elements of ArrayList<HashMap<String, String>> that has been sorted by a comparator?

I am getting some data via JSON which I store in a Hashmap map e.g. map.put(KEY_TITLE, parser.getString("title")) and then add the map to an ArrayList> bookList. This is displayed in a list view with bookList as source for the adapter. Additionally I also store all the book info (title, author, published date) of the map in Arraylist arrTitles, arrAuthors, arrDates as I need to pass these values to my next activity. My problem is when I use a comparator to sort the bookList on basis of titles then the bookList gets sorted just fine. I however want to sort my arrTitles, arrAuthors and arrDates as per the new sorting of the bookList so that my arrTitles.getItem(position) sends the correct value to the next activity. Any idea how I can do this? Below are the hashmap, arraylist, onItemClick & comparator code:

 for (int i = 0; i < json.Length(); i++) {
        HashMap<String, String> map = new HashMap<String, String>();
        // adding each child node to HashMap key => value
        map.put(KEY_ID, json.getString("id"));
        map.put(KEY_TITLE, json.getString("title"));
        map.put(KEY_AUTHOR, json.getString("author"));
        map.put(KEY_DATE, json.getString("date"));
        arrTitles.add(json.getString("title");
        arrAuthors.add(json.getString("author");
        arrDates.add(json.getString("date");
        //add map to ArrayList
        booksList.add(map);
}

        list=(ListView)findViewById(R.id.list);
        Collections.sort(booksList, mapComparator);
        adapter=new LazyAdapter(this, booksList);        
        list.setAdapter(adapter);

        // Click event for single list row
        list.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                int position, long id) {
        Intent intent = new Intent(this, editList.class);
        intent.putExtra("title", arrTitles.getItem(position));
        intent.putExtra("date", arrDates.getItem(position));
        startActivity(intent);


        }
    }); 
 } 

       public Comparator<Map<String, String>> mapComparator = new Comparator<Map<String, String>>() {
       public int compare(Map<String, String> m1, Map<String, String> m2) {
       return m1.get(KEY_TITLE).compareTo(m2.get(KEY_TITLE));
    }
};
Jon Skeet
people
quotationmark

Right, with the new code it seems reasonably simple: just create the individual lists after you've sorted the list of maps:

// This loop looks wrong at the moment - you're not using i
for (int i = 0; i < json.Length(); i++) {
     HashMap<String, String> map = new HashMap<String, String>();
     // adding each child node to HashMap key => value
     map.put(KEY_ID, json.getString("id"));
     map.put(KEY_TITLE, json.getString("title"));
     map.put(KEY_AUTHOR, json.getString("author"));
     map.put(KEY_DATE, json.getString("date"));
     booksList.add(map);
}

for (Map<String, String> map : booksList) {
     arrTitles.add(map.get(KEY_TITLE));
     arrAuthors.add(map.get(KEY_AUTHOR));
     arrDates.add(map.get(KEY_DATE));
}

Note that I would still strongly recommend creating a Book class to store the data in, instead of using a Map<String, String> with fixed keys. It'll be a lot simpler to work with elsewhere in the code, I suspect.

people

See more on this question at Stackoverflow