通过实现可比较排序自定义对象ArrayList

时间:2015-08-24 08:17:05

标签: java list arraylist collections

我正在尝试排序arrayList包含自定义对象(DrugClass)。请找到以下代码

要求: 任何药物名称不含药物(param)应首先显示。其他药物名称包含的药物(param1 ... param2..param..10)应按降序显示。请帮帮我

 package com.demo.test2;

    public class DrugClass implements Comparable<DrugClass> {



    public DrugClass(int quantity,String name){

        super();
        this.quantity=quantity;
        this.drugName=name;
    }
        private String drugName;
        private int quantity;

        public String getDrugName() {
            return drugName;
        }
        public void setDrugName(String drugName) {
            this.drugName = drugName;
        }
        public int getQuantity() {
            return quantity;
        }
        public void setQuantity(int quantity) {
            this.quantity = quantity;
        }
        @Override
        public int compareTo(DrugClass o) {
            // TODO Auto-generated method stub
            String compareName = o.getDrugName();
            if(compareName.contains("param")){
                return compareName.compareTo(this.drugName);
            }

            return this.drugName.compareTo(compareName);
        }


    }


Sorting class :

package com.demo.test2;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

    public class SortDrugClass {

        /**
         * @param args
         */
        public static void main(String[] args) {
            // TODO Auto-generated method stub
            List<DrugClass> drugList=new ArrayList<DrugClass>();

            DrugClass d1=new DrugClass(1, "aDrug param1");
            DrugClass d2=new DrugClass(2, "bDrug param2");
            DrugClass d3=new DrugClass(3, "cDrug param3");
            DrugClass d4=new DrugClass(4, "dDrug");


            drugList.add(d1);
            drugList.add(d2);
            drugList.add(d3);
            drugList.add(d4);

            Collections.sort(drugList);

            int i=0;
            for(DrugClass temp: drugList){
               System.out.println("Drugs " + ++i + " : " + temp.getDrugName() + 
                ", Quantity : " + temp.getQuantity());
            }
        }

    }


    This is giving me below output:

        Drugs 1 : cDrug param3, Quantity : 3
        Drugs 2 : bDrug param2, Quantity : 2
        Drugs 3 : aDrug param1, Quantity : 1
        Drugs 4 : dDrug, Quantity : 4

    The actual Requirement is:

         Drugs 4 : dDrug, Quantity : 4
            Drugs 1 : cDrug param3, Quantity : 3
            Drugs 2 : bDrug param2, Quantity : 2
            Drugs 3 : aDrug param1, Quantity : 1

2 个答案:

答案 0 :(得分:0)

    @Override
    public int compareTo(DrugClass o) {
        String compareName = o.getDrugName();
        if(!compareName.contains("param") && this.drugName.contains("param"))
            return -1;            
        return this.drugName.compareTo(compareName);
    }

答案 1 :(得分:0)

您必须处理compareTo中的四个案例:

  • 这有param,其他没有(返回1,其他先行)
  • 这个没有param,其他有(返回-1,这先行)
  • 两者都没有param(按升序排序)
  • 都有param(按降序排序)

以下是实施:

@Override
public int compareTo(DrugClass o) {
    boolean hasParam = drugName.contains("param");
    boolean oHasParam = o.drugName.contains("param");

    if(hasParam) {
        return oHasParam ? o.drugName.compareTo(drugName) : 1;
    }
    return oHasParam ? -1 : drugName.compareTo(o.drugName);
}