Informatheus
Informatheus

Reputation: 1025

Sorting with Comparator not working

I have to store and manage a volume list in the format: "100 ml", "200 ml", "300 ml"...

I'm using the SharedPreferences with JSON to store this everytime the list is changed.

I want the list to be ordered, so 100 < 1000, but it is showing like 1000 and 2000 before 300.

Here is my comparator:

mVolumeComparator = new Comparator<String>() {
        @Override
        public int compare(String s1, String s2) {

            int volume1 = Integer.parseInt(s1.replace(" ml", ""));
            int volume2 = Integer.parseInt(s2.replace(" ml", ""));

            if (volume1 > volume2) {
                return 1;
            } else if (volume2 > volume1) {
                return -1;
            } else {
                return 0;
            }
        }
    };

And here is my get method:

public static ArrayList<String> getVolumesFromPreference(Context ctx) {

    if (!ctx.getSharedPreferences(KEY_SHARED_PREFERENCES, MODE_PRIVATE).contains(KEY_VOLUMES_BUNDLE)) {

        startDefaultVolumes(ctx, KEY_VOLUMES_BUNDLE);

    }

    try {
        JSONArray jsonArray = new JSONArray(ctx.getSharedPreferences(KEY_SHARED_PREFERENCES, MODE_PRIVATE).getString(KEY_VOLUMES_BUNDLE, null));
        ArrayList<String> lista = new ArrayList<>();

        for (int i = 0; i < jsonArray.length(); i++) {
            lista.add(jsonArray.getString(i));
        }

        Collections.sort(lista, mVolumeComparator);

        return lista;
    } catch (JSONException e) {
        e.printStackTrace();
        return null;
    }


}

 private static void startDefaultVolumes(Context ctx, String key_bundle) {

    JSONArray jsonArray = new JSONArray();
    jsonArray.put("269 ml");
    jsonArray.put("350 ml");
    jsonArray.put("473 ml");
    jsonArray.put("550 ml");
    jsonArray.put("600 ml");

    SharedPreferences.Editor editor = ctx.getSharedPreferences(KEY_SHARED_PREFERENCES, MODE_PRIVATE).edit();
    editor.putString(key_bundle, jsonArray.toString());
    editor.commit();

}

Volumes Lib

Upvotes: 1

Views: 549

Answers (3)

Maya Mohite
Maya Mohite

Reputation: 683

Try this code for sorting arraylist in ascending order.

 ArrayList<int> lista = new ArrayList<>();

        for (int i = 0; i < jsonArray.length(); i++) {
            lista.add(Integer.parseInt(jsonArray.getString(i)));
        }

 Collections.sort(lista , new Comparator<Integer >() {
            @Override
            public int compare(Integer lhs, Integer rhs) {
                return Integer.valueOf(lhs).compareTo(Integer.valueOf(rhs));
            }
        });

Upvotes: 0

alorma
alorma

Reputation: 954

I will recommend you to store only the value "100", "1000", "300" and UNIT if you have more than unit available.

SO, you can order it just with a normal Integer comparator, and then apply the "ml" suffix at runtime

Upvotes: 1

s_klepcha
s_klepcha

Reputation: 399

use Integer.valueOf() method instead Integer.parceInt()

Upvotes: 2

Related Questions