Java: How to sort an ArrayList

Hi,

I am creating a small Android app and I want to sort an ArrayList…
It is an ArrayList of the class Skill.
I implemented the Comparable interface like this:
Skill = public class(Comparable)

But when I try to sort my arraylist like this:
Collections.sort(fList);

I get this compiler error:
Error 1 (E210) Generic parameter doesn’t validate on “java.lang.Comparable<java.lang.Object>” constraint for “T” C:\Documents and Settings\Jeroen\Mijn documenten\Visual Studio 2010\Projects\org.me.rpassistant\org.me.rpassistant\MainActivity.pas 104 19 org.me.rpassistant

What am I doing wrong here?

Did you try Comparable<Skill> as a base type?

Hi Carlo,

I just tried this small Java console app:

namespace consoleapplication2;

interface

uses
  java.util;

type
  ConsoleApp = class
  public
    class method Main(args: array of String);
  end;

  Skill = class(Comparable)
  private
  public
    property Name: String;
    method compareTo(arg1: consoleapplication2.Skill): RemObjects.Oxygene.System.Integer;
    property Value: Int32;
  end;

implementation

method Skill.compareTo(arg1: consoleapplication2.Skill): RemObjects.Oxygene.System.Integer;
begin
  result := Name.compareTo(arg1.Name);
end;

class method ConsoleApp.Main(args: array of String);
begin
  var lst := new ArrayList;
  Collections.sort(lst);
end;

end.

And I still get this error…

Best Regards,

Jeroen

  java.util.Collections.sort(lst);

does it. This uses java’s “reverse” constraints so it cannot be inferred.

I wonder why I have to call a static method on a utility class to sort a generic collection?
Why can’t I just call myList.sort();
At some points the .net class lib is MUCH better designed than the Java one…

Lack of extension methods. There’s a feature request for that, even properly speced. I’m presuming it will show some time in java 8 or 9.