0

I have an ArrayList<HashMap<String,String>> and I want to sort it. My ArrayList output in Logcat is like this:

[{num=0, username=p, startPoliPro=A,  finalPoliPro=B, diff=0},
 {num=1, username=e, startPoliPro=C,  finalPoliPro=D,  diff=548.0Km},
 {num=2, username=e, startPoliPro=E,  finalPoliPro=F, diff=3.0Km}]

I want to sort the list based on "diff" value by ascending order so that Logcat has to be like:

[{num=0, username=p, startPoliPro=A,  finalPoliPro=B, diff=0},
 {num=2, username=e, startPoliPro=E,  finalPoliPro=F, diff=3.0Km},
 {num=1, username=e, startPoliPro=C,  finalPoliPro=D,  diff=548.0Km}]

I have read many similar topics and tried something like

Collections.sort(final_itinList, new Comparator<HashMap< String,String >>() {

    @Override
    public int compare(HashMap<String, String> lhs, HashMap<String, String> rhs) {
        // Do your comparison logic here and retrn accordingly.
        return lhs.get("diff").compareTo(rhs.get("diff"));                      
    }
});

with no success. Any help would be appreciated

2 Answers 2

1

Currently, you are trying to compare two String Objects:

return lhs.get("diff").compareTo(rhs.get("diff"));

What you really want to do is comparing the returned Integers, so you would need to do something like this:

return (Integer.parseInt(lhs.get("diff")) - Integer.parseInt(rhs.get("diff")));
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for reply. Given that distance is in double format i changed your code to Double.parseDouble but i got error " The return type is incompatible with Comparator<HashMap<String,String>>.compare(HashMap<String,String>, HashMap<String,String>)". Any suggestion;
Solved. Many thanks. I use "return Double.compare(Double.parseDouble(lhs.get("diff")), Double.parseDouble(rhs.get("diff")));"
0

Your Comparator is comparing two Strings. That's probably why the list is not sorted correctly. The "diff" string should be parsed as an integer (or float) to compare it.

If your objects always have the same structure, I would advise to create a List of a custom object (where the diff is an integer representing the number of kilometers) instead of using a List of Maps. In that case, you could make your custom object implement Comparable.

Something like :

public class MyCustomObject implements Comparable<MyCustomObject> {

    private String mNum;

    private String mUsername;

    private String mStartPoliPro;

    private String mFinalPoliPro;

    private int mDiff;

    @Override
    public int compareTo(MyCustomObject another) {
        return mDiff - another.getDiff();
    }

    public String getNum() {
        return mNum;
    }

    public void setNum(String num) {
        mNum = num;
    }

    public String getUsername() {
        return mUsername;
    }

    public void setUsername(String username) {
        mUsername = username;
    }

    public String getStartPoliPro() {
        return mStartPoliPro;
    }

    public void setStartPoliPro(String startPoliPro) {
        mStartPoliPro = startPoliPro;
    }

    public String getFinalPoliPro() {
        return mFinalPoliPro;
    }

    public void setFinalPoliPro(String finalPoliPro) {
        mFinalPoliPro = finalPoliPro;
    }

    public int getDiff() {
        return mDiff;
    }

    public void setDiff(int diff) {
        mDiff = diff;
    }
}

and then simply call

List<MyCustomObject> myList = // create your object list
Collections.sort(myList);

4 Comments

I'm afraid the OP doesn't wants to extend HashMap
And the OP never said he is using JSON, he just showed the output of ArrayList.toString()
Didn't say to extend HashMap. But the data structure List<Map> doesn't seem right on that particular example. It seems that the OP actually wants a List<Object> that he can sort. The Json part is a guess (maybe a bad one)
It doesn't seem that the OP wants to have a List<Object>. I think what he has is okay, I would change it to List<Properties>, but that actually doesn't matter

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.