0

Example

    private static final Comparator<A> PRODUCT_CODE_COMPARATOR = new Comparator<A>()
{
    @Override
    public int compare(final A o1, final A o2)
    {
        return o1.getCode().compareTo(o2.getCode());
    }
};
public static <T extends A> List<T> sortProductsByCode(final Collection<T> productModels)
{
    return sortProducts(productModels, PRODUCT_CODE_COMPARATOR);
}

private static <T> List<T> sortProducts(final Collection<T> t, final Comparator<T> comparator)
{
    final List<T> variants = new ArrayList<T>(t);
    Collections.sort(variants, comparator);
    return variants;
}

Getting an error at return sortProducts(productModels, PRODUCT_CODE_COMPARATOR);

Can anyone help?

1 Answer 1

2

You need your declaration of sortProducts to be:

private static <T> List<T> sortProducts(final Collection<T> t, 
                                        final Comparator<? super T> comparator)

This allows the Comparator to compare T's or any super class of T. Or, in other words, the method will accept Comparator<T> or Comparator<A>.

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

Comments

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.