How to declare a sort function in Java / Anylogic?

437 Views Asked by At

I am a little lost. What's the syntax to declare a sort function in a class ?

I have my class here :

public class PLClass 
{
     double PL;
     int index;

/** other functions **/
}

And I want to sort a collection of elements PLClass with order based on the element double PL:

public ArrayList<PLClass> Mycollec;
Collections.sort(Mycollec, Collections.reverseOrder(PL));

I get the following error message : Description: The method reverseOrder(Comparator) in the type Collections is not applicable for the arguments (double).

Any idea what I am missing ? Thanks !

1

There are 1 best solutions below

1
On BEST ANSWER

If your main intention is to only sort a collection of PLClass based on the element PL then the following might be what you are looking for

    Collections.sort(Mycollect,new Comparator<PLClass>(){
        @Override
       public int compare(PLClass o1, PLClass o2) {
          Double i = o1.PL;
          Double j = o2.PL;
          return i.compareTo(j);
       };  
    };