Fenia Kechagia
Fenia Kechagia

Reputation: 83

Sorting ArrayList Hashmap

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

Upvotes: 0

Views: 198

Answers (2)

Julien Arzul
Julien Arzul

Reputation: 971

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);

Upvotes: 0

msrd0
msrd0

Reputation: 8361

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")));

Upvotes: 1

Related Questions