代码之家  ›  专栏  ›  技术社区  ›  stackFan

分类行为不同?

  •  3
  • stackFan  · 技术社区  · 8 年前

    当我尝试使用 Comparator.naturalOrder()

    List< String > ordered = Arrays.asList( "This", "is", "the", "natural" ,"order");
    
    System.out.println( "Natural order" );
    
    ordered.forEach( System.out::println );
    
    ordered.sort(Comparator.naturalOrder( ));
    
    System.out.println( "After ordering" );
    
    for ( String string: ordered ) {
        System.out.println( string );
    }
    

    输出:

    Natural order
    This
    is
    the
    natural
    order
    
    After ordering
    This
    is
    natural
    order
    the
    

    为什么是 Comparator.reverseOrder() .

    2 回复  |  直到 7 年前
        1
  •  6
  •   Eugene    8 年前

    naturalOrder 方法 根据 Comparator 或普通字符串比较顺序, 源头的遭遇令。这是完全不同的事情。

    可能是一股 Integer 更容易理解:

    Stream.of(3,4,1,2)...
    

    遭遇命令是 3, 4, 1, 2

    排序顺序为 1, 2, 3, 4 -意义 已排序(通过 Comparator.naturalOrder()

        2
  •  1
  •   Naman    8 年前

    naturalOrder() 返回一个 Comparator Comparable 自然秩序中的物体。

    为每个字母输入ASCII值 ).