Java 申请排序和选择

Java application for sorting and selecting

我需要开发一个简单的应用程序来根据预定义的规则对数据进行排序和 selecting。应用程序必须能够处理 JSON 个任意结构的对象列表,select 个包含具有特定值的键的对象,并且还可以使用自然排序顺序按值对对象进行排序。

我制作了一个带有“包含”规则的应用程序,该规则接受一组 key:value 对来检查条目的匹配项。我该如何排序?


    static class DataJSON{

        public condition condition;

        public ArrayList<data> data;
        public class exclude{
            public String name;
            public Integer rating;
            public Boolean disabled;
            public String user;
        }
        public class include{
            public String name;
            public Integer rating;
            public Boolean disabled;
            public String user;
        }
        public class data{
            public Boolean disabled;
            public Integer rating;
            public String user;
            public String name;
        }
        public class condition{
            public ArrayList<exclude> exclude;
            public ArrayList<include> include;
            public ArrayList<String> sort_by;
        }
    }
    static class resultPrepare{
        public Boolean disabled;
        public Integer rating;
        public String user;
        public String name;
        public resultPrepare(Boolean disabled, Integer rating, String user, String name){
            this.disabled = disabled;
            this.rating = rating;
            this.user = user;
            this.name = name;
        }
    }
    static class DataResult{
        //public Test.result result;
        public static ArrayList<result> result;
        public DataResult(ArrayList<result> result){
            this.result = result;
        }
    }
    public static class result{
        public Boolean disabled;
        public Integer rating;
        public String user;
        public String name;
        public result(Boolean disabled, Integer rating, String user, String name){
            this.disabled = disabled;
            this.rating = rating;
            this.user = user;
            this.name = name;
        }
    }

    public static void main(String[] args){
        String json = "{\"data\": [{\"user\": \"mike@mail.com\", \"rating\": 20, \"disabled\": false, \"name\": \"Ton1\"},\n" +
                "{\"user\": \"greg@mail.com\", \"rating\": 14, \"disabled\": false, \"name\": \"Ton\"},\n" +
                "{\"user\": \"john@mail.com\", \"rating\": 25, \"disabled\": false, \"name\": \"T\"}],\n" +
                "\"condition\": {\"include\": [{\"name\": \"Ton1\"}],\"exclude\": [{\"disabled\": false, \"name\": \"Tonf\"},{\"disabled\": false, \"name\": \"T\"}], \"sort_by\": [\"rating\"]}}";
        Gson gson = new Gson();
        DataJSON jsParse = gson.fromJson(json, DataJSON.class);
        ArrayList<result> res = new ArrayList<result>();
        for(int i = 0; i < jsParse.data.size(); i++){
            Boolean flagInclude = false;
            Boolean flagExclude = false;
            Boolean flagExcludeDisabled = false;
            if(jsParse.condition.include != null){
                for(int j = 0; j < jsParse.condition.include.size(); j++){
                    if(jsParse.data.get(i).name != null && jsParse.condition.include.get(j).name != null){
                        if(jsParse.data.get(i).name.equalsIgnoreCase(jsParse.condition.include.get(j).name) || jsParse.condition.include.get(j).name == null){
                            if(jsParse.data.get(i).disabled == jsParse.condition.include.get(j).disabled || jsParse.condition.include.get(j).disabled == null){
                                if(jsParse.data.get(i).rating == jsParse.condition.include.get(j).rating || jsParse.condition.include.get(j).rating == null){
                                    if((jsParse.data.get(i).user!= null && jsParse.condition.include.get(j).user != null)|| jsParse.condition.include.get(j).user == null){
                                        if(jsParse.data.get(i).user.equalsIgnoreCase(jsParse.condition.include.get(j).user) || jsParse.condition.include.get(j).user == null){
                                            flagInclude = true;
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }else{
                flagInclude = true;
            }
            if(jsParse.condition.exclude != null){
                for(int j = 0; j < jsParse.condition.exclude.size(); j++){
                    if(jsParse.data.get(i).name != null && jsParse.condition.exclude.get(j).name != null){
                        if(jsParse.data.get(i).name.equalsIgnoreCase(jsParse.condition.exclude.get(j).name) || jsParse.condition.exclude.get(j).name == null){
                            if(jsParse.data.get(i).disabled == jsParse.condition.exclude.get(j).disabled || jsParse.condition.exclude.get(j).disabled == null){
                                if(jsParse.data.get(i).rating == jsParse.condition.exclude.get(j).rating || jsParse.condition.exclude.get(j).rating == null){
                                    if((jsParse.data.get(i).user!= null && jsParse.condition.exclude.get(j).user != null)|| jsParse.condition.exclude.get(j).user == null){
                                        if(jsParse.data.get(i).user.equalsIgnoreCase(jsParse.condition.exclude.get(j).user) || jsParse.condition.exclude.get(j).user == null){
                                            flagExclude = true;
                                            break;
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }else{
                flagExclude = false;
            }
            System.out.println("flagInclude:  " + flagInclude + "  flagExclude:  " + flagExclude);
            if(flagInclude == true && flagExclude == false){
                result r = new result( jsParse.data.get(i).disabled, jsParse.data.get(i).rating , jsParse.data.get(i).user, jsParse.data.get(i).name );
                res.add(r);
            }
        }
        DataResult dataResult = new DataResult(res);
        String jsParse2 = gson.toJson(dataResult.result);
        String outJson = "{\"result\":" + jsParse2 + "}";
        System.out.println(outJson);
    }
}

输出:“result”:[{“disabled”:false,“rating”:20,“user”:“mike@mail.com”,“name”:“Ton1”}]}

查看 java Comparator 接口和 List[=20] 的 sort 方法=] 界面。有了它,您可以为 'result' class 创建自定义比较器(顺便说一句。class 名称应始终以大写开头)。

您的案例的名称比较器示例。您需要实施缺失的部分,并根据您的规则集选择使用哪一个。

// Add me somewhere in your code
public static class NameComparator implements Comparator<Result>
{
    @Override
    public int compare(Result o1, Result o2) {
        return o1.name.compareTo(o2.name);
    }
}

// And in the block where you output the json structure:
DataResult dataResult = new DataResult(res);
DataResult.result.sort(new NameComparator()); // <-- I am new
String jsParse2 = gson.toJson(dataResult.result);
String outJson = "{\"result\":" + jsParse2 + "}";
System.out.println(outJson);

希望对您有所帮助。