0

我有一个名为 x 的类,它是一个数组列表,需要按值降序排序。我的课-

 public static class x
{
    public int id;
    public double value;
    public x(int _id, double _value)
    {
        id = _id;
        value = _value;
        //System.out.println(Integer.toString(id));
    }

    public Integer getID(){
        return id;
    }
    public double getValue(){
        return value;
    }


    //Sorting
    public static Comparator<x> getComparator(SortParameter... sortParameters) {
        return new xComparator(sortParameters);
    }

    public enum SortParameter {
        VAL_DESCENDING
    }

    private static class xComparator implements Comparator<x> {
        private SortParameter[] parameters;

        private xComparator(SortParameter[] parameters) {
            this.parameters = parameters;
        }

        public int compare(x o1, x o2) {
            int comparison;
            for (SortParameter parameter : parameters) {
                switch (parameter) {
                   case VAL_DESCENDING:
                        comparison = o2.id - o1.id;
                        if (comparison != 0) return comparison;
                        break;

                }
            }
            return 0;
        }
    }

}

我这样称呼它:

  cp = x.getComparator(x.SortParameter.VAL_DESCENDING);
  Collections.sort(attr1, cp);

attr1是我的数组列表

仅供参考,我正在关注这个

我收到错误:

cannot find symbol : variable cp    

我是java的新手:(

4

1 回答 1

0

尝试使用Comparator<x> cp = x.getComparator(x.SortParameter.VAL_DESCENDING);来声明它。在声明变量之前,您不能使用它

于 2013-03-08T03:57:26.933 回答