简体   繁体   English

根据长度升序对字符串数组进行排序

[英]Sort an array of strings based on length in ascending order

The question is to print the array of string in accordance with the length of strings in ascending order.问题是按照字符串的长度升序打印字符串数组。

For example例如

input={"vellore","i","from","am"}
output=i am from vellore

Here is my code:这是我的代码:

int n=sc.nextInt();
    sc.nextLine();
    String[] arr = new String[n];
    for(int i=0;i<n;i++){
        arr[i]=sc.nextLine();
    }
    //System.out.println(Arrays.toString(arr));
    Arrays.sort(arr);
    for(String i: arr){
        System.out.print(i+" ");
    }

Now I know that my output will come in lexical order that is as "am from i vellore", but I want to get my desired output using sort method.现在我知道我的 output 将以“我来自我维洛尔”的词汇顺序出现,但我想使用排序方法获得我想要的 output。 I tried using Collections.sort() as well by using arraylist but I still didn't get my desired output.我尝试使用 Collections.sort() 以及使用 arraylist 但我仍然没有得到我想要的 output。

I want to get my output using sort method without using the normal approach by comparing the lengths of string and all.我想通过比较字符串和所有的长度来使用排序方法而不使用常规方法来获取我的 output。

You want a Comparator .你想要一个Comparator In this case, you specifically want to start by comparing the String lengths.在这种情况下,您特别希望从比较String长度开始。 I would suggest you then compare naturally (to break ties).我建议你然后自然地比较(打破关系)。 Like,喜欢,

String[] arr = { "vellore", "i", "from", "am" };
Arrays.sort(arr, Comparator.comparingInt(String::length)
        .thenComparing(Comparator.naturalOrder()));
System.out.println(Arrays.toString(arr));

Outputs (as requested)输出(按要求)

[i, am, from, vellore]

tl;dr tl;博士

Comparator
.comparingLong( ( String s ) -> s.codePoints().count() )
.thenComparing( Comparator.naturalOrder() )

Avoid legacy type char避免遗留类型char

The Answer by Frisch is correct in suggesting the use of a Comparator . Frisch 的答案在建议使用Comparator时是正确的。 However, the method reference seen there, String::length , fails with most characters.但是,那里看到的方法参考String::length对大多数字符都失败了。 See this example .请参阅此示例 The String#length method reports a two-character string like c as three, incorrectly. String#length方法将两个字符的字符串(如c )错误地报告为三个。

The failure is because that method depends on char which has been legacy since Java 2. As a 16-bit value, the char type is physically incapable of representing most of the over 140,000 characters defined in a Unicode.失败是因为该方法依赖于自 Java 2 以来一直存在的char 。作为 16 位值, char类型在物理上无法表示 Unicode 中定义的超过 140,000 个字符中的大部分。

Code points代码点

Instead, use Unicode code point integers.相反,使用 Unicode 代码点整数。

Switch out that method reference for the use of code points rather than char type.切换出该方法参考以使用代码点而不是char类型。

Here we use String#codePoints to generate an IntStream .这里我们使用String#codePoints来生成一个IntStream That IntStream is a stream of each character's code point number, a value in the range of zero to just over a million.IntStream是每个字符的代码点编号的 stream,其值在零到略超过一百万的范围内。 Then we use IntStream#count to get a count of the values in that stream, a count of the characters in the original string.然后我们使用IntStream#count来获取 stream 中的值的计数,即原始字符串中字符的计数。

    String[] arr = { "a1", "c🕒", "b2", "d4" };
    Arrays
        .sort(
            arr , 
            Comparator
            .comparingLong( ( String s ) -> s.codePoints().count() )
            .thenComparing( Comparator.naturalOrder() )
        );
    System.out.println( Arrays.toString( arr ) );

See that code run live at IdeOne.com .请参阅在 IdeOne.com 上实时运行的代码

[a1, b2, c, d4] [a1,b2,c,d4]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM