java - JavaFx ObservableList<String> sorted() vs sorted(Comparator.<String>naturalOrder()) -
so tried using
listofstrings = listofstrings.sorted(); ordering user11, user10, user20, user04 etc... which has javadoc states create list natural ordering
user complained ordering thought have write comparator luck intellij auto filled in
listofstrings = listofstrings.sorted(comparator.<string>naturalorder()); ordering user01, user02, user03, user04 etc... my first thought return same thing sorts strings want. documentation comparator.naturalorder natural order.
so did miss in documentation?
my reading of documentation leads me believe should order list same. understand why don't?
strings being read in text file user01,user02,user03.user04,user05,user06,user07,user08,user09,user10,user11,user12,user13,user14,user15,user16,user17,user18,user19,user20 used keys in has map.
for (string user: usermap.keyset()) { listofstrings.add(user); } // listofstrings = listofstrings.sorted(); // listofstrings = listofstrings.sorted(comparator.<string>naturalorder());
it bug in java 8u40 has subsequently been (mostly) fixed later releases. (see comments on answer discussion of edge cases or alternate implementation choices).
the bug tracker issue:
and associated changeset.
the java 8u40 code in observablelist.java is:
public default sortedlist<e> sorted() { return sorted(null); } the erroneous call sequence above call detailed in user pbabcdefp's answer. error in code means sorted() method doesn't return sorted list @ all, instead returns list in current order in.
the java 8u-dev (current trunk code) , java 9u-dev code is:
/** * creates {@link sortedlist} wrapper of list 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); }
Comments
Post a Comment