I have google’d it, and tried some examples, but I always get stuck.
This is the error message, when I try to use the Collections.sort:
The generic method
sort(List<T>)of typeCollectionsis not applicable for the arguments (ArrayList<HashMap<String,String>>). The inferred typeHashMap<String,String>is not a valid substitute for the bounded parameter<T extends Comparable<? super T>>.
My code is similar to this one.
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
//Get the data (see above)
JSONObject json = JSONfunctions.getJSONfromURL("http://xxxxxxxxxxxxxxxxxxxxxx");
try {
//Get the element that holds the earthquakes ( JSONArray )
JSONArray earthquakes = json.getJSONArray("earthquakes");
//Loop the Array
for (int i = 0; i < earthquakes.length(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
JSONObject e = earthquakes.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put("name", "Earthquake name:" + e.getString("eqid"));
map.put("magnitude", "Magnitude: " + e.getString("magnitude"));
mylist.add(map);
}
} catch (JSONException e) {
Log.e("log_tag", "Error parsing data " + e.toString());
}
ListAdapter adapter = new SimpleAdapter(this, mylist, R.layout.main,
new String[]{"name", "magnitude"},
new int[]{R.id.item_title, R.id.item_subtitle});
setListAdapter(adapter);
As already explained by @pcalcao, you need to decide how you are going to sort the maps (based on what criteria), then implement that sort order in a custom comparator and call Collections.sort with that comparator.
For example, let’s say you want to sort by alphabetical order of names, you would write something like: