如何实现链式比较器

时间:2016-02-23 00:26:23

标签: java list sorting object comparator

我正在创建一个程序,用参数对对象的ArrayList进行排序:

  

字符串名称
  int类型(1-3)
  int diffs(代表难度,1-3))
  并在几天(截止日期前的几天)。

我想按顺序依次使用所有参数对对象列表进行排序。我只是习惯了比较器,并想知道我将如何在我的代码中实现比较器链。 我知道有其他方法可以做到这一点,比如在一个比较器中使用if else语句或者使用compareToBuilder,但我不确定哪个是最好的,或者我还应该考虑其他替代方法。

主要代码:

import java.io.*;
import java.util.*;

public class InputItem
{
  public int row;
  public static void main(String args[])
  {

    String again;
    String names[] = new String[100];
    int types[] = new int[100];
    int diffs[] = new int[100];
    int days[] = new int[100];
    int row=0;
    do{
    System.out.println("Please input assignment name:");
    Scanner newNames = new Scanner(System.in);
    String name = newNames.nextLine();
    names[row] =name;

    System.out.println("Please input assignment type:");
    Scanner typeI = new Scanner(System.in);
    int type = typeI.nextInt();
    types[row] = type;

    System.out.println("Please input assignment difficulty:");
    Scanner diffI = new Scanner(System.in);
    int diff = diffI.nextInt();
    diffs[row] = diff;
   // input days...
    System.out.println("Would you like to add another item? Enter 'Yes' or 'No'");
    Scanner input = new Scanner(System.in);
      again = input.next();
      row++;
    }
    while(again.equalsIgnoreCase("Yes"));
    List<Itemss> WORK = new ArrayList<Itemss>();
    for(int count = 0; count<row; count++)
    {
      WORK.add(new Itemss(((types[count])), (names[count])));
    }
    Collections.sort(WORK, new COMP());

    System.out.println("Sorted List Entries: ");
    for(Itemss a: WORK)
    {
      System.out.println(a);
    }
  }
}

Itemss类和比较器的代码

import java.util.*;
class COMP implements Comparator<Itemss>
{
  @Override  //overides compareTo() method 
  public int compare(Itemss a1, Itemss a2)
  {
    if((a1).getType()< (a2).getType())
    {
        return 1;
    } 
    else
    {
      return -1;
    }
  }
}
public class Itemss
{
  private String name;
  private int type;
  //private int diff;  
  //private int days;

  public Itemss(int t, String n)
  {
    name = n;
    type = t;
    //diff = df;
    //days = da;
  }
  public String getName()
  {
    return name;
  }
  public void setName(String name)
  {
    this.name = name;
  }
  public int getType()
  {
    return type;
  }
  public void setType(int type)
 {
   this.type = type;
  }
  public String toString()
 {   
    return this.name + "-->Type:" + this.type ;
  }
}

2 个答案:

答案 0 :(得分:0)

遵循是一种基本方法。您以前的比较器严格比较大于或小于。要链接,在当前变量相等时比较下一组变量。这是一个例子:

class COMP implements Comparator<Items> {
    @Override // overides compareTo() method
    public int compare(Items a1, Items a2) {
        if (a1.getType() < a2.getType()) {
            return 1;
        } else if (a1.getType() > a2.getType()) {
            return -1;
        } else if (a1.getDiff() < a2.getDiff()) {
            return 1;
        } else if (a1.getDiff() > a2.getDiff()) {
            return -1;
        } else if (a1.getDays() < a2.getDays()) {
            return 1;
        } else if (a1.getDays() > a2.getDays()) {
            return -1;
        }
        return 0;
    }
}

创建一个示例输出,如:

-- AFTER SORT --
Items [name=Item 8, type=3, diff=3, days=5]
Items [name=Item 9, type=3, diff=2, days=4]
Items [name=Item 7, type=3, diff=1, days=3]
Items [name=Item 4, type=2, diff=3, days=10]
Items [name=Item 5, type=2, diff=2, days=6]
Items [name=Item 6, type=2, diff=1, days=12]
Items [name=Item 3, type=1, diff=2, days=11]
Items [name=Item 1, type=1, diff=2, days=10]
Items [name=Item 2, type=1, diff=1, days=9]

答案 1 :(得分:0)

你应该使用这样的东西:

public void order(List<MyObject> myList) {

    Comparator<MyObject> byName = new Comparator<MyObject>() {
        @Override
        public int compare(MyObject o1, MyObject o2) {
            if (o1.getName() != null && o2.getName() != null) {
                return o1.getName().compareToIgnoreCase(o2.getName());
            }
            return -1;
        }
    };

    Comparator<MyObject> byType = new Comparator<MyObject>() {
        @Override
        public int compare(MyObject o1, MyObject o2) {
            if (o1.getType() != null && o2.getType() != null) {
                return o1.getType().compareTo(o2.getType());
            }
            return -1;
        }
    };

    Comparator<MyObject> byDiffs = new Comparator<MyObject>() {
        @Override
        public int compare(MyObject o1, MyObject o2) {
            if (o1.getDiffs() != null && o2.getDiffs() != null) {
                return o1.getDiffs().compareTo(o2.getDiffs());
            }
            return -1;
        }
    };

    Comparator<MyObject> byDays = new Comparator<MyObject>() {
        @Override
        public int compare(MyObject o1, MyObject o2) {
            if (o1.getDays() != null && o2.getDays() != null) {
                return o1.getDays().compareTo(o2.getDays());
            }
            return -1;
        }
    };

    ComparatorChain chain = new ComparatorChain();
    chain.addComparator(byName);
    chain.addComparator(byType);
    chain.addComparator(byDiffs);
    chain.addComparator(byDays);

    Collections.sort(myList, chain);

}