0

Can you guide me how can I sort Array List having Hash Map alphabatically?

JSONArray   jArr2 = new JSONArray(jsonString2);

for(int i=0;i<jArr2.length();i++){

HashMap<String, String> map = new HashMap<String, String>();
map.put("titleName",jArr2.getJSONObject(i).getString("titleName"))
programList.add(map);

}
7
  • try to use COllections.Sort() in java? Commented Sep 5, 2013 at 9:17
  • Use a TreeMap with appropriate comparator? Commented Sep 5, 2013 at 9:20
  • what does it mean to sort a list of maps? Are you interested in just a single key ("titleName")? Commented Sep 5, 2013 at 9:20
  • Collections.Sort will not solve this directly as it compare objects and not values. Commented Sep 5, 2013 at 9:21
  • Please clarify your requirement a little further. Your ArrayList contains HashMap. You are talking abut sorting HashMap alphabetically? Or what? Commented Sep 5, 2013 at 9:21

3 Answers 3

2

Implement a Comparator<HashMap<String, String>> which just extracts the value assocated with the value key, then use Collections.sort method to sort your arraylist.

For e.g.:

class MyComparator implements Comparator<Map<String, String>>{

    private final String key;

    public MyComparator(String key)
    {
        this.key = key;
    }

    public int compare(Map<String, String> first,
                       Map<String, String> second)
    {
        // TODO: Null checking, both for maps and values
        String firstValue = first.get(key);
        String secondValue = second.get(key);
        return firstValue.compareTo(secondValue);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Do accept the answer if that helps you to solve your question. Please refer how does accepting answer works: meta.stackexchange.com/questions/5234/…
1

Looking at your example, I don't think you need a Map to be involved at all. What you have is a list of Maps, where every Map only has one key, which is "titleName". Why not just have a list of titlenames? Then your code would look like this:

JSONArray jArr2 = new JSONArray(jsonString2);

List<String> titleNames = new ArrayList<>();

for (int i = 0; i < jArr2.length(); i++) {

    titleNames.add(jArr2.getJSONObject(i).getString("titleName"))

}

You know that the list only contains titleNames, you don't need to complicate the data structure with Maps!

Then you can sort the list simply by using

Collections.sort(titleNames);

Note that this will work while the other answers that suggests Collections.sort() on the list of maps will not work. This is because titleNamees is a List of Strings, which implement Comparable (ie the sort() method knows how to order them with respect to each other), while Map does not implement comparable (as there are multiple ways to order Maps - number of entries, total number of bytes, etc).

Comments

0

Yes you can use Collections.sort(); with a custom comparator. Here is the doc.

Collections.sort(YOUR_ARRAY_LIST, new YourCustomComparator());

And this should be the class you must have

class YourCustomComparator implements Comparator<HashMap<String, String>> {

    @Override
    public int compare(HashMap<String, String> lhs, HashMap<String, String> rhs) {

        // check here your objects. lhs and rhs. compare them as you want
        // return 1 if lhs is greater than rhs
        // return 0 if ther are same
        // return -1 otherwise
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.