JavaFx ObservableList <String> sorted () vs sorted (Comparator. <String> naturalOrder ())

So I initially tried to use

listOfStrings = listOfStrings.sorted();


Ordering user11, user10, user20, user04 etc...

What has a JavaDoc that indicates that it will create a list with a natural order

The user complained about the order, so I thought that I would have to write a comparator, but, fortunately, IntelliJ auto fills

listOfStrings = listOfStrings.sorted(Comparator.<String>naturalOrder());

Ordering user01, user02, user03, user04 etc...

My first thought was that it would return the exact same thing, but it sorts the rows as I want. The documentation for Comparator.naturalOrder is also of natural order .

So what did I miss in the documentation?

My reading of the documentation makes me think that they should order the list the same way. Does anyone understand why they are not doing this?

user01, user02, user03.user04, user05, user06, user07, user08, user09, user10, user11, user12, user13, user14, USER15, user16, user17, user18, user19, user20 .

for (String user: userMap.keySet()) {
            listOfStrings.add(user);
}  

// listOfStrings = listOfStrings.sorted();  //
listOfStrings = listOfStrings.sorted(Comparator.<String>naturalOrder());
+4
2

Java 8u40, ( ) ​​ . (. ).

:

.


Java 8u40 ObservableList.java:

public default SortedList<E> sorted() {
    return sorted(null);
}

pbabcdefp. , sorted() , , .

Java 8u-dev ( ) Java 9u -dev:

/**
 * Creates a {@link SortedList} wrapper of this list with the natural
 * ordering.
 * @return new {@code SortedList}
 * @since JavaFX 8.0
 */
public default SortedList<E> sorted() {
    Comparator naturalOrder = new Comparator<E>() {

        @Override
        public int compare(E o1, E o2) {
            if (o1 == null && o2 == null) {
                return 0;
            }
            if (o1 == null) {
                return -1;
            }
            if (o2 == null) {
                return 1;
            }

            if (o1 instanceof Comparable) {
                return ((Comparable) o1).compareTo(o2);
            }

            return Collator.getInstance().compare(o1.toString(), o2.toString());
        }
    };
    return sorted(naturalOrder);
}
+3

, . , , .

ObservableList.sorted()

public default SortedList<E> sorted() {
    return sorted(null);
}

.

public default SortedList<E> sorted(Comparator<E> comparator) {
    return new SortedList<>(this, comparator);
}

.

public SortedList(ObservableList<? extends E> source,
              Comparator<? super E> comparator)

SortedList, . . null, .

, , , ObservableList . . , ?

+2

Source: https://habr.com/ru/post/1584545/


All Articles