how to specify that T implement Comparable?

I get error: Cannot resolve method 'sort(java.util.List<T>, boolean)' in case:

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Arrays;

public class MyClass {
    public static void main(String args[]) {
        MyArray arr = new MyArray();
        arr.set("b");
        arr.set("a");
        arr.sort();
    }
}
class MyArray<T> {
    List<T> myList;
    
    MyArray(){
        myList = new ArrayList<T>();
    }
    
    public void set(T val) {
        myList.add(val);
    }
    
    public void sort() {
        ListFunctions.sort(myList, true);
        System.out.println(myList);
    }
}

class ListFunctions {
    public static <T extends Comparable<T>> void sort(List<T> array, boolean ascending){
        if (ascending){
            array.sort(null);
        }
        else {
            array.sort(Collections.reverseOrder());
        }
    }
}

Problem is in line ListFunctions.sort(myList, true). I can’t convert this way (List)myList, because meaning of constraint T extends Comparable<T> is lost. I see examples of code where T need to implement Comparable interface, but in my case I can’t specify class. I implicitly use List<String>

Answer

Your class

class MyArray<T>

Should simply be

class MyArray<T extends Comparable<T>>

If you don’t do that, you may potentially pass an out of bound type to your utility function which the compiler will reject accordingly.

MyArray arr = new MyArray();

will hence become

MyArray<String> arr = new MyArray<>();