Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用array.sort()按长度对字符串数组进行排序_Java_Arrays - Fatal编程技术网

Java 如何使用array.sort()按长度对字符串数组进行排序

Java 如何使用array.sort()按长度对字符串数组进行排序,java,arrays,Java,Arrays,我试图使用Arrays.sort(),根据字符串的长度对字符串数组进行排序,但这是按字典顺序而不是按长度对字符串进行排序。这是我的密码: S = "No one could disentangle correctly" String W[] = S.split(" "); Arrays.sort(W); 排序后: correctly could disentangle no one 但我想要的是 no //length = 2 one //length = 3 could //length

我试图使用
Arrays.sort()
,根据字符串的长度对字符串数组进行排序,但这是按字典顺序而不是按长度对字符串进行排序。这是我的密码:

S = "No one could disentangle correctly"
String W[] = S.split(" ");
Arrays.sort(W);
排序后:

correctly
could
disentangle
no
one
但我想要的是

no  //length = 2
one //length = 3
could //length = 4 and likewise
correctly
disentangle
如何获得上述输出?请给出JDK1.7和JDK1.8的答案。

用于java 8及以上版本

 Arrays.sort(W, (a, b)->Integer.compare(a.length(), b.length()));

更简洁的方法是从这里使用Comparator.comparingit

替代matt的版本,并且比matt的版本稍微简单

Arrays.sort(W, Comparator.comparingInt(String::length));
如果您使用的是JDK1.8或更高版本,那么可以使用lambda表达式,如mattanswer。但如果您使用的是JDK 1.7或更早版本,请尝试编写一个自定义比较器,如下所示:

String S = "No one could disentangle correctly";
String W[] = S.split(" ");
Arrays.sort(W, new java.util.Comparator<String>() {
    @Override
    public int compare(String s1, String s2) {
        // TODO: Argument validation (nullity, length)
        return s1.length() - s2.length();// comparision
    }
});
String S=“没有人能正确解开”;
字符串W[]=S.split(“”);
sort(W,new java.util.Comparator(){
@凌驾
公共整数比较(字符串s1、字符串s2){
//TODO:参数验证(nullity,length)
返回s1.length()-s2.length();//比较
}
});
导入java.util.*;
索特斯特林加里级
{
公共静态void main(字符串[]args)引发java.lang.Exception
{
String S=“没有人能正确解开”;
字符串W[]=S.split(“”);
sort(W,新的StringLengthComparator());
用于(字符串str:W)
System.out.println(str);//打印预期结果。
}
}
类StringLengthComparator根据需要实现Comparator{//自定义Comparator类
@凌驾
公共整数比较(字符串str1、字符串str2){
返回str1.length()-str2.length();//比较字符串的长度
}
}

只需更改参数位置,即可按降序排序

Arrays.sort(W, (a,b)->b.length() - a.length());

Java8非常简单。如上述答案所述。 我在hackerrank上解决了一些问题,他们正在使用Java7。所以我不得不写java7。 这就是选择排序。虽然时间复杂度不是很高,但O(n^2)可以达到这个目的

public static void main(String[] args) {
    // I am taking list and again converting to array. please ignore. you can directly take array of          string and apply the logic.
        List<String> listOfString = new ArrayList<String>();
        listOfString.add("because");
        listOfString.add("can");
        listOfString.add("do");
        listOfString.add("must");
        listOfString.add("we");
        listOfString.add("what");
        String[] w= new String[listOfString.size()];

        for(int i =0;i <listOfString.size();i++) {
            w[i] = listOfString.get(i);
        }
        // That is for java 8
        //Arrays.sort(w, (a, b)->Integer.compare(a.length(), b.length()));

        for (int i = 0; i < w.length; i++) {
                for(int j=i+1;j<w.length;j++) {
                    String tempi = w[i];
                    String tempj = w[j];

                    if(tempj.length()<tempi.length()) {
                        w[i] =w[j];
                        w[j]=tempi;
                    }
                }
        }

       // That is for printing the sorted array
        for (int i = 0; i < w.length; i++) {
            System.out.println(w[i]);
        }

创建您自己的比较器。@matt所以您的意思是我不能通过数组实现这一点。sort()不,我的意思是使用2参数版本的Arrays.sort(Object[],comparator)。Arrays.sort()按字母顺序对字符串进行排序。您希望它使用什么样的排序标准?这是您需要编写自己的比较器的部分。您是否按长度排序?这个答案可能会有所帮助:这也可以通过使用更完整的比较器打开<代码>Comparator.comparingit(字符串::长度)。然后比较(Comparator.naturalOrder())如何按降序获取它?
Comparator.comparingInt(字符串::长度)。reversed()
public static void main(String[] args) {
    // I am taking list and again converting to array. please ignore. you can directly take array of          string and apply the logic.
        List<String> listOfString = new ArrayList<String>();
        listOfString.add("because");
        listOfString.add("can");
        listOfString.add("do");
        listOfString.add("must");
        listOfString.add("we");
        listOfString.add("what");
        String[] w= new String[listOfString.size()];

        for(int i =0;i <listOfString.size();i++) {
            w[i] = listOfString.get(i);
        }
        // That is for java 8
        //Arrays.sort(w, (a, b)->Integer.compare(a.length(), b.length()));

        for (int i = 0; i < w.length; i++) {
                for(int j=i+1;j<w.length;j++) {
                    String tempi = w[i];
                    String tempj = w[j];

                    if(tempj.length()<tempi.length()) {
                        w[i] =w[j];
                        w[j]=tempi;
                    }
                }
        }

       // That is for printing the sorted array
        for (int i = 0; i < w.length; i++) {
            System.out.println(w[i]);
        }
do
we
can
must
what
because