0

i have a array list having the array of ratting i want to sort ratting in ascending order. according to the ratting i want data in array list will also sort

   static ArrayList<data1> al=new ArrayList<data1>();
static class data1
{

  int ratting;
   String retailer_image1;
   String retailer_image2;
   String retailer_image3;
   String retailer_image4;
   String retailer_image5;
   String retailer_image6;
   String retailer_image7;
   String retailer_image8;
   String retailer_image9;
   String retailer_image10;
   String retailer_image11;
   String retailer_image12;
   int id;

   data1(int id,double lati,double longi,String path,String address,String city,String telephone,String distance,String title,String image, String retailer_image1, String retailer_image2, String retailer_image3, String retailer_image4, String retailer_image5, String retailer_image6, String retailer_image7, String retailer_image8, String retailer_image9, String retailer_image10, String retailer_image11, String retailer_image12,int ratting,String zip)
   {
       this.image=image;
       this.retailer_image1=retailer_image1;
       this.retailer_image2=retailer_image2;
       this.retailer_image3=retailer_image3;
       this.retailer_image4=retailer_image4;
       this.retailer_image5=retailer_image5;
       this.retailer_image6=retailer_image6;
       this.retailer_image7=retailer_image7;
       this.retailer_image8=retailer_image8;
       this.retailer_image9=retailer_image9;
       this.retailer_image10=retailer_image10;
       this.retailer_image11=retailer_image11;
       this.retailer_image12=retailer_image12;
       this.ratting=ratting;
       this.zip=zip;

   }
} 
  • If that's as creative as your name needs to be, why not just have an ArrayList of Strings for your images? That constructor is crazy; frankly, so is a class named `data1`. – ChiefTwoPencils Jul 12 '14 at 04:39
  • when variable names only differ by a number, you should consider using an array or ArrayList instead. – Code-Apprentice Jul 12 '14 at 05:04

2 Answers2

0

Maybe this will help you

public class CustomComparator implements Comparator<MyObject> {
    @Override
    public int compare(MyObject o1, MyObject o2) {
        return o1.getStartDate().compareTo(o2.getStartDate());
    }
}

or reffer this question for more answers

Community
  • 1
  • 1
Darko Petkovski
  • 3,892
  • 13
  • 53
  • 117
0

Try this..

public class Sortrattingadapter implements Comparator<data1> {

    @Override
    public int compare(data1 lhs, data1 rhs) {

        if(lhs.getratting() > rhs.getratting())
        {
            return 1;
        }
        else if(lhs.getratting() < rhs.getratting())
        {
            return -1;
        }
        else if(lhs.getratting() == rhs.getratting())
        {
            return lhs.getretailer_image1().compareTo(rhs.getretailer_image1());
        }

        return 0;
    }

}

and before setting adapter to ListView add below line

Collections.sort(al, new Sortrattingadapter());
Hariharan
  • 24,741
  • 6
  • 50
  • 54