Skip to content

[Bug] The parameters of method CollectionUtils#sort should be comparable type #16235

@codingkiddo

Description

@codingkiddo

Pre-check

  • I am sure that all the content I provide is in English.

Search before asking

  • I had searched in the issues and found no similar issues.

Apache Dubbo Component

Java SDK (apache/dubbo)

Dubbo Version

Dubbo - 3.3, JDK - 21, OS - MacOS(26.3.1 (a) (25D771280a))

Steps to reproduce this issue

Improve generic type safety of CollectionUtils.sort

What you expected to happen

Motivation

CollectionUtils.sort currently accepts any List<T> and internally calls Collections.sort using a raw cast / suppressed warnings.

This allows code like CollectionUtils.sort(List<NonComparableType>) to compile, but it may fail at runtime because natural ordering requires elements to implement Comparable.

Proposed change

Change the method signature from:

public static <T> List<T> sort(List<T> list)

to

public static <T extends Comparable<? super T>> List<T> sort(List<T> list)

and remove the raw cast

Collections.sort(list);

Anything else

This aligns the method with java.util.Collections.sort, improves compile-time type safety, and preserves the original return type List.

Do you have a (mini) reproduction demo?

  • Yes, I have a minimal reproduction demo to help resolve this issue more effectively!

Are you willing to submit a pull request to fix on your own?

  • Yes I am willing to submit a pull request on my own!

Code of Conduct

Metadata

Metadata

Assignees

No one assigned

    Labels

    type/enhancementEverything related with code enhancement or performance

    Type

    No type
    No fields configured for issues without a type.

    Projects

    Status

    Todo

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions