1

How can i sort Arraylist of LinkedHashMap
Ex.

ArrayList<LinkedHashMap<String, String>> list = new ArrayList<LinkedHashMap<String, String>>();
        LinkedHashMap<String, String> linkedHashMap = new LinkedHashMap<String, String>();
        linkedHashMap.put("city", "AB");
        linkedHashMap.put("name", "N");
        linkedHashMap.put("area", "xyz");
        list.add(linkedHashMap);
        linkedHashMap = new LinkedHashMap<String, String>();
        linkedHashMap.put("city", "BA");
        linkedHashMap.put("name", "AB");
        linkedHashMap.put("area", "xyz");
        list.add(linkedHashMap);
        linkedHashMap = new LinkedHashMap<String, String>();
        linkedHashMap.put("city", "CH");
        linkedHashMap.put("name", "AB");
        linkedHashMap.put("area", "PQ");
        list.add(linkedHashMap);

Above is my ArrayList of LinkedHashMap & i want to Sort Value taking Ref as Key (like Sort By City or Sort by name)
I have Done LinkedHashMap(Without Arraylist) Sorting by using Collection.Sort & comparable .
But how can i Sort My Arraylist of LinkedHashMap(Using Loop or Iterator it may possible but is there any other simple way?) Thanks

2
  • ArrayList<T> can be sorted only if T implements Comparable interface. So you need to design a custom LinkedHashMap class implementing Comparable and then use Collections.sort() to sort them. Commented Mar 7, 2012 at 5:35
  • I would suggest using entities(pojo) instead of linked hash map of storing your object it help you implement comparator. you can find dozen of links by simple google search 'how to sort ArrayList in java'. hope this will help. Commented Mar 7, 2012 at 5:44

2 Answers 2

3

You can implement the Comparator<T> and use Collections.sort to sort by the particular value of your LinkedHashMap


However, in you case, you should take advantage of class. Make a class that represent the data inLinkedHashMap

public class MyClass {
    private String city;
    private String name;
    private String area;

    // implement some getters here.
}

Then, again, use Collections.sort, and implement the comparator you want

List<MyClass> myClasses = getMyClasses();
Collections.sort(myClasses, new Comparator<MyClass>(){
    @Override
    public int compare(MyClass o1, MyClass o2) {
        // implement sorting behavior you want.
        return 0;
    }
});
Sign up to request clarification or add additional context in comments.

Comments

1

Alternatively you can do as follows:

    Collections.sort(list ,
            new Comparator<LinkedHashMap<String, String>>()
            {
                @Override
                public int compare(LinkedHashMap<String, String> obj1,
                        LinkedHashMap<String, String> obj2)
                {
                    return (obj1.get("city").compareTo(obj2.get("city")));
                }
            });

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.