简体   繁体   English

在两个字段上对自定义对象数组进行排序

[英]sorting custom object array on two fields

I'm sorting an array of custom objects ( ListData[] ) on two fields. 我正在对两个字段上的自定义对象数组( ListData[] )进行排序。 I want it to be sorted by theme, and them by name. 我希望按主题排序,并按名称排序。 I thought i made a nice comparator in the custom object class and that i could use Arrays.sort(ld) to make my code working and sorting my array. 我以为我在自定义对象类中做了一个不错的比较器,可以使用Arrays.sort(ld)使我的代码正常工作并对数组进行排序。 But apparently im doing something wrong... 但显然我在做错事...

my custom object: 我的自定义对象:

   public class ListData implements Comparable<ListData>{
public int venueID;
public String name;
public String photoUrl;
public String tip;
public String theme;
@Override
public int compareTo(ListData ld0) {
    return this.venueID- ld0.venueID;
}

public static Comparator<ListData> ListDataThemeAndNameComparator = new Comparator<ListData>() {
    @Override
    public int compare(ListData ld1, ListData ld2) {

        String compareTheme1 = ld1.theme.toUpperCase();
        String compareTheme2= ld2.theme.toUpperCase();
        String compareName1 = ld1.name.toUpperCase();
        String compareName2= ld2.name.toUpperCase();

        //ascending
        int comp = compareTheme1.compareTo(compareTheme2); // comp themes
        if(comp==0){ // same theme
            comp= compareName1.compareTo(compareName2); // compare names 
        }
        return comp;
        }
};


}

And in my main activity i have: 在我的主要活动中,我有:

 ListData ld[]= new ListData[jsonResponse.size()];
(some code filling my ListData array)
 Arrays.sort(ld, ListData.ListDataThemeAndNameComparator); // compare by theme and then by name

Does anyone know what i'm doing wrong? 有人知道我在做什么错吗?

I edited my code But still it fails, now on a nullpointerexception on the compareTheme1 = ld1.theme.toUpperCase(); 我编辑了代码,但是仍然失败,现在在compareTheme1 = ld1.theme.toUpperCase();上出现了nullpointerexception . But i am sure my array is not empty, i logged it the line before sorting it and its filled with about 500 items. 但是我确定我的数组不是空的,我在对其进行排序之前记录了该行,并填充了约500个项目。

Your ListData object should implements Comparable not Comparator interface. 您的ListData对象应该实现Comparable not Comparator接口。

EDIT:

To make things clear, you can sort an array by Array.sort() . 为了清楚Array.sort() ,您可以按Array.sort()array进行排序。 To make custom sort, you can specify your comparator in Array.sort() , if you don't do that, array will be sorted in natural order which you can define by implementing Comparable interface. 要进行自定义排序,可以在Array.sort()指定比较器,如果不这样做,则将按照自然顺序对数组进行排序,可以通过实现Comparable接口来定义数组。 So you have two options how to custom sort: 因此,您有两种方法可以自定义排序:

  • by using custom comparator and specifying it in Array.sort() 通过使用自定义比较器并在Array.sort()指定它
  • by implementing Comparable interface to your items 通过对您的商品实施Comparable接口

I would suggest you to go with implementing Comparable . 我建议您继续实施Comparable You save memory by not creating new comparator objects and Comparator is useful if you are comparing objects of different types which is not your case. 通过不创建新的比较器对象来节省内存,如果比较的不是您的情况,则Comparator器很有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM