数组列表中的排序问题

时间:2014-07-12 04:36:42

标签: android arrays

我有一个数组列表,其中包含一系列的鼠标,我想按升序排序。 根据鼠标我想要数组列表中的数据也将排序

   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;

   }
} 

2 个答案:

答案 0 :(得分:0)

也许这会对你有所帮助

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

或提出this问题以获得更多答案

答案 1 :(得分:0)

试试这个..

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

}

在将适配器设置为ListView之前添加以下行

Collections.sort(al, new Sortrattingadapter());