0

I have a class called x which is a array list and needs to be sorted in Decreasing order by Value. My Class-

 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;
        }
    }

}

I Call it like:

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

attr1 is my array list

Just for Reference I am following this

I am getting error:

cannot find symbol : variable cp    

I am a newbie to java :(

Community
  • 1
  • 1
Umesh Moghariya
  • 3,063
  • 6
  • 21
  • 23

1 Answers1

0

try using Comparator<x> cp = x.getComparator(x.SortParameter.VAL_DESCENDING); to declare it. you can not use a variable until it is declared

vishal_aim
  • 7,636
  • 1
  • 20
  • 23