Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8191430

List.sort should specify the sort is stable

XMLWordPrintable

    • Icon: CSR CSR
    • Resolution: Approved
    • Icon: P4 P4
    • 10
    • core-libs
    • None
    • behavioral
    • low
    • Hide
      There is a small risk that 3rd party List implementations override the default sort implementation with a non-stable sort. However, if such Lists are passed as an argument to Collections.sort then the contract is broken, as such this compatibility concern overrides that of existing List implementations.
      Show
      There is a small risk that 3rd party List implementations override the default sort implementation with a non-stable sort. However, if such Lists are passed as an argument to Collections.sort then the contract is broken, as such this compatibility concern overrides that of existing List implementations.
    • Java API
    • SE

      Summary

      Specify the List.sort method to be a stable sort.

      Problem

      Collections.sort specifies the sort is stable, but it defers to List.sort. This contradicts the specification.

      Solution

      Specify stability of the sort in List.sort.

      Specification

      Update the specification on List.sort:

           /**
            * Sorts this list according to the order induced by the specified
      -     * {@link Comparator}.
      +     * {@link Comparator}.  The sort is <i>stable</i>: this method must not
      +     * reorder equal elements.
            *
            * <p>All elements in this list must be <i>mutually comparable</i> using the
            * specified comparator (that is, {@code c.compare(e1, e2)} must not throw

            psandoz Paul Sandoz
            psandoz Paul Sandoz
            Alan Bateman
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: