0

I write a program that parse json and shows it in a listView.
Now i want to sort it but don't know how and fail.
In program i have a List<PoolOfflineModal> alist = new ArrayList<>(); so modal are this;

public class PoolOfflineModal {
    private int id;
    private String name;
    private int price;
    private int priceWithoutDiscount;
    private String tel;
    private int discountPercent;
    private String address;
    private String photo;
}

and data simple is here http://hayat.cloudsite.ir/pools/pools.json

so i want to sort List ascending with price, so update ListView and i don't know...
another idea?! or solution?!

another way are exist to sort ListView from a column without sort list?!?

Allan Pereira
  • 2,572
  • 4
  • 21
  • 28
Mahdi
  • 923
  • 1
  • 6
  • 17
  • 1
    Possible duplicate of [Sort ArrayList of custom Objects by property](http://stackoverflow.com/questions/2784514/sort-arraylist-of-custom-objects-by-property) – Mike M. Apr 13 '16 at 12:37

2 Answers2

2

this is my last Comparator

    public class MyComparator implements Comparator<PoolOfflineModal> {
    @Override
    public int compare(PoolOfflineModal lo, PoolOfflineModal ro) {
        int result = 0;
        if (way == "name") {
            result = lo.getName().compareTo(ro.getName());
        } else if (way == "price") {
            result = lo.getPrice().compareTo(ro.getPrice());
        } else if (way == "percent") {
            result = lo.getDiscountPercent().compareTo(ro.getDiscountPercent());
        }
        return result;
    }
}
Mahdi
  • 923
  • 1
  • 6
  • 17
0

List Object Like This:

List<PoolOfflineModal> alist = new ArrayList<>();

Comparator Class Like this:

public class MyComparator implements Comparator<PoolOfflineModal> {
    @Override
    public int compare(PoolOfflineModal o1, PoolOfflineModal o2) {
        return o1.id.compareTo(o2.id);
    }
}

Use Like this:

Collections.sort(alist, new MyComparator());

Hope it will helpful to you.

Darshak
  • 2,298
  • 4
  • 23
  • 45
  • it have a error in compareTO, "Cannot invoke compareTo(int) on the primitive type int" – Mahdi Apr 13 '16 at 13:09
  • a lot <3 to You Darshak, Thanks, i used before, but don't answerd, now i changed type of price in "json,modal,and other where" it's worked and listview sorted. a way is exist to sort int?! or i must set all int's to string?!? – Mahdi Apr 13 '16 at 13:20
  • yes solved, i forced to use strings insted of integer. in Comparor i don't succeed to sort from int objects. – Mahdi Apr 17 '16 at 20:33