0

I have a Object:

public class ListAnnotationsOrigin implements Serializable {

/**
 * 
 */
private static final long serialVersionUID = 8443084209405942551L;

@JsonProperty("opcombo")
private List<opcombo> listComboOp;

@JsonProperty("vicombo")
private List<vicombo> listComboVi;

@JsonProperty("vicombo")
private String vicombo;

@JsonProperty("viversion")
private String idVersionVi;

@JsonProperty("closed")
private String closed;

@JsonProperty("params")
private List<SegmentOrigin> listSegmentOrigin; // <-- here

   // getters,setters etc.

I want sort listSegmentOrigin

@Entity
@Table(name = "SegmentOrigin")
public class SegmentOrigin implements Comparable<T>{ // <-- ERROR 

 ....

    @JsonProperty("idSegment")
    private String idSegment;

    public int compareTo(SegmentOrigin arg0) {
      if (this.getIdSegment().compareTo(arg0.getIdSegment())) { // <<--ERROR type mismatch: cannot convert from int to boolean
        return 1;
      } else {
        return 0;   
      }
    }
}

In my controller->

ListAnnotationsOrigin list = this.getListAnnotationsOrigin ();
return Collections.sort((List<SegmentOrigin>) list.getListSegmentOrigin());

I send my List from ListAnnotationsOrigin in an orderly manner, Then when I implements Comparable{ I get red line "implements methods unics" I import -> public int compareTo(SegmentOrigin arg0) { ... and I get other error, thanks.

9
  • 1
    compareTo() returns an int, and not a boolean. return this.getIdSegment().compareTo(arg0.getIdSegment()) should do the work. Commented Oct 23, 2018 at 15:34
  • Please attach the error stacktrace Commented Oct 23, 2018 at 15:34
  • 2
    At Comparable<T> instead of T use type which should be compared, in your case SegmentOrigin. Commented Oct 23, 2018 at 15:34
  • @Benoit Your suggested solution looks right, but problem description isn't. OP isn't returning boolean, problem here is that if(condition) expect at condition expression which could be evaluated as boolean, but what OP has evaluates to int. Commented Oct 23, 2018 at 15:37
  • OK, I changed T for SegmentOrigin and changed my if. Now I get error in my return from controller. "Cannot return a void result" Commented Oct 23, 2018 at 15:42

1 Answer 1

2

Your entity should look like this :

@Entity
@Table(name = "SegmentOrigin")
public class SegmentOrigin implements Comparable<SegmentOrigin>{

    @JsonProperty("idSegment")
    private String idSegment;

    public int compareTo(SegmentOrigin arg0) {
        return this.getIdSegment().compareTo(arg0.getIdSegment()); // Check nullity if needed
    }
}

implements Comparable<SegmentOrigin> means that your object can be compared with another SegmentOrigin object.

Sign up to request clarification or add additional context in comments.

1 Comment

OK, I changed T for SegmentOrigin and changed my if. Now I get error in my return from controller. "Cannot return a void result"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.