0

I am trying to sort ArrayList on the basis of String actually I have numbers in "", like this: "4000"

I have written code to sort arraylist on the basis of price, but whenever i do tap on button it is not affecting to my ListView... why ?

I am following this tutorial....

This is how my JSON looks:

{
"locations": [
{
"name": "Office",
"price": "4,00,000"
},
{
"name": "Work",
"price": "1,20,000"
}
]
}

Model class:

public class Locations {

    private String name;
    private String price;

    public Locations() {
        // TODO Auto-generated constructor stub
    }

    public Locations(String name, String price) {
        super();
        this.name = name;
        this.price = price;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPrice() {
        return price;
    }

    public void setPrice(String price) {
        this.price = price;
    }

}

LocationsActivity.java:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_locations);
    actorsList = new ArrayList<Locations>();
    new JSONAsyncTask().execute(" ");

    listview = (ListView) findViewById(R.id.list);
    adapter = new LocationsAdapter(getApplicationContext(), R.layout.adapter_locations, actorsList);

    btnHTL = (Button) findViewById(R.id.btnHTL);
    btnHTL.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Collections.sort(actorsList, new Comparator<Locations>(){
                  public int compare(Locations obj1, Locations obj2)
                  {
                      // ascending
                      return (Integer)(arg1.price).compareTo(arg2.price);
                  }
                });
            }
        }); 

    btnLTH = (Button) findViewById(R.id.btnLTH);
    btnLTH.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            Collections.sort(actorsList, new Comparator<Locations>(){
                  public int compare(Locations obj1, Locations obj2)
                  {
                      // descending
                      return (Integer)(arg2.price).compareTo(arg1.price);
                  }
                }); 
            }
        });
}
6
  • So what's the question? I don't get what you're trying to do. By reading your code the first thing i would suggest you is to process the result in your onPostExecute, not in your doInBackground, just left doInBackground for background network operations Commented Aug 11, 2014 at 9:16
  • how to filter records - simply want to show records based on price High to Low and Low to High Commented Aug 11, 2014 at 9:17
  • sort your arraylist by price. Commented Aug 11, 2014 at 9:18
  • @DivyangMetalia exactly this is what i want to know !' Commented Aug 11, 2014 at 9:18
  • I believe this might help mkyong.com/java/… You have to define an order to your array based on what it contains and what you want to order, so you can sort it by using the sort() function Commented Aug 11, 2014 at 9:21

5 Answers 5

1
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button asc = (Button) findViewById(R.id.asc);
        Button desc = (Button) findViewById(R.id.desc);
        final ArrayList<Location> actorsList = new ArrayList<Location>();

        Location loc = new Location();
        loc.setName("1");
        loc.setPrice("4000");
        actorsList.add(loc);

        loc = new Location();
        loc.setName("2");
        loc.setPrice("8000");
        actorsList.add(loc);

        loc = new Location();
        loc.setName("3");
        loc.setPrice("1000");
        actorsList.add(loc);

        loc = new Location();
        loc.setName("4");
        loc.setPrice("16000");
        actorsList.add(loc);

        asc.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Collections.sort(actorsList, new Comparator<Location>() {
                    @Override
                    public int compare(Location arg1, Location arg2) {
                        Integer obj1 = new Integer(arg1.getPrice().replace(",",""));
                        Integer obj2 = new Integer(arg2.getPrice().replace(",",""));
                        return (Integer) (obj1).compareTo(obj2);
                    }
                });

                for (int i = 0; i < actorsList.size(); i++) {
                    System.out.println("monika 1233"
                            + actorsList.get(i).getPrice());
                }
            }
        });

    }

}
Sign up to request clarification or add additional context in comments.

7 Comments

i want to implement both the functionalities, a button to show list High to Low and another to show list Low to High
but I want to implement sort functionality on click of buttons, see my code above
yes thats what creat a method in pojo class and that tell whether u want asc or dec order sorting
so in compare method above provide if else hence that will give u sorting data
best to do is : always sort in asc and do Arrays.sort(data, Collections.reverseOrder());
|
0

You can use Collections.sort, which takes a comparator in argument. In the comparator you will define what you want to compare (here, the price).

Comments

0

If you just want to sort the list by price, use custom comparator. If you want filter the list based on any search term, use Filterable interface

Comments

0

Make class like this:

class PriceSort implements Comparator<location> {
    @Override
    public int compare(location cr1, location cr2) {

          long l1 = Integer.parseInt(cr1.getPrice());
          long l2 = Integer.parseInt(cr2.getPrice());
          return Long.compare(l1, l2);

    }
}

And call like:

Collections.sort(actorsList, new PriceSort());

Comments

0

I added code for int sorting:

Collections.sort(actorsList, new Comparator<Locations>() {
    public int compare(Locations user1, Locations user2) {
        return user1.price.compareTo(user2.price);
    }
});

And implement Comparable method in your Location model:

Locations implements Comparable<Locations>

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.