在 Java 中按逆字母顺序对字符串数组进行排序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13779643/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 13:57:11  来源:igfitidea点击:

Sorting an array of strings in reverse alphabetical order in Java

java

提问by Quacky

I've been tasked with turning this code into a reverse sort, but for the life of me cannot figure out how to do it. These are my sort, findlargest and swap methods. I have a feeling I am missing something glaringly obvious here, any help would be really appreciated.

我的任务是将这段代码转换为反向排序,但我一生都无法弄清楚如何去做。这些是我的 sort、findlargest 和 swap 方法。我有一种感觉,我在这里遗漏了一些明显的东西,任何帮助将不胜感激。

    public static void sort(String[] arr)
    {
        for (int pass = 1; pass < arr.length; pass++)
        {
            int largestPos = findLargest(arr, arr.length - pass);
            if (largestPos != arr.length - pass)
            {
                swap(arr, largestPos, arr.length - pass);
            }
        }
    }

    public static int findLargest(String[] arr, int num)
    {
        int largestPos = 0;
        for (int i = 1; i <= num; i++)
        {
            if (arr[i].compareToIgnoreCase(arr[largestPos]) > 0)
            {
                largestPos = i;
            }
        }
        return largestPos;
    }

    public static void swap(String[] arr, int first, int second)
    {
        String temp = arr[first];
        arr[first] = arr[second];
        arr[second] = temp;
    }
}

回答by arshajii

Don't reinvent the wheel -

不要重新发明轮子-

String[] strs = {"a", "b", "d", "c", "e"};

Arrays.sort(strs, Collections.reverseOrder(String.CASE_INSENSITIVE_ORDER));

System.out.println(Arrays.toString(strs));
[e, d, c, b, a]

回答by blitzen

Follow up from A. R. S.'s answer:

跟进 ARS 的回答:

You could use a custom comparator if you are allowed to use the Arrays.Sort method...

如果允许使用 Arrays.Sort 方法,则可以使用自定义比较器...

Arrays.sort(stringArray, new Comparator<String>() {
            @Override
            public int compare(String t, String t1) {
                return -t.compareToIgnoreCase(t1); //reverse the comparison, while ignoring case
            }
        });

回答by Kamil

Can you just turn findLargest to findSmallest, like this:

你能不能把 findLargest 变成 findSmallest,像这样:

public static void sort(String[] arr) {
    for (int pass = 1; pass < arr.length; pass++) {
        int largestPos = findSmallest(arr, arr.length - pass);
        if (largestPos != arr.length - pass) {
            swap(arr, largestPos, arr.length - pass);
        }
    }
}

public static int findSmallest(String[] arr, int num) {
    int largestPos = 0;
    for (int i = 1; i <= num; i++) {
        if (arr[i].compareToIgnoreCase(arr[largestPos]) < 0) {
            largestPos = i;
        }
    }
    return largestPos;
}

public static void swap(String[] arr, int first, int second) {
    String temp = arr[first];
    arr[first] = arr[second];
    arr[second] = temp;
}

回答by Terry Li

public static void sort(String[] arr) {
     Arrays.sort(arr);
     for (int i=0; i<arr.length/2; i++) {
        swap(arr,i,arr.length-1-i);
     }
}

Try this one if you want. In your version you are moving the largest towards the end of the array, resulting in alphabetical order.

如果你愿意,试试这个。在您的版本中,您将最大的移到数组的末尾,从而按字母顺序排列。

Just in case you insist on your original approach, I have made some minor changes to your code:

以防万一你坚持你原来的方法,我对你的代码做了一些小改动:

public static void sort(String[] arr)
{
    for (int pass = 1; pass < arr.length; pass++)
    {
        int largestPos = findLargest(arr, pass-1);
        if (largestPos != pass - 1)
        {
            swap(arr, largestPos, pass - 1);
        }
    }
}

public static int findLargest(String[] arr, int num)
{
    int largestPos = num;
    for (int i = num+1; i < arr.length; i++)
    {
         if (arr[i].compareToIgnoreCase(arr[largestPos]) > 0)
         {
            largestPos = i;
         }
    }
    return largestPos;
}

The most trivial one though, as suggested by Ian Roberts, is simply Arrays.sort(arr, Collections.reverseOrder());.

不过,正如伊恩·罗伯茨( Ian Roberts)所建议的那样,最简单的就是Arrays.sort(arr, Collections.reverseOrder());.

回答by someone

I think This is the one you need (if you don't think about collection framework).

我认为这是您需要的(如果您不考虑集合框架)。

public static void main(String args[]) {


    String [] arr ={"abc","bac","cbc"};
            String temp="";

    for(int i=0;i<arr.length;i++){

        for(int j=i+1;j<arr.length;j++){

            if(arr[j].compareTo(arr[i]) > 0){

                temp = arr[i] ;
                arr[i] = arr[j];
                arr[j] = temp;
            }
        }

    }

    for(String val:arr){
        System.out.println(val);
    }

}

Output is

输出是

cbc
bac
abc

回答by Subin Sebastian

you can use Arrays.sort(arr)to sort in alphabetical order.

您可以使用Arrays.sort(arr)按字母顺序排序。

and then reverse it.

然后反转它。

回答by Subin Sebastian

So, first we need to create String array, then use Arrays.sort(String[]);, then use forto reverse sort array to reverse order.

所以,首先我们需要创建 String 数组,然后使用Arrays.sort(String[]);,然后使用for反向排序数组来反向排序。

import java.util.Arrays;

public class SortClass {
    public static void main(String[] args) {
        String[] arrayString = new String[5];
        arrayString[0] = "Cat";
        arrayString[1] = "Apple";
        arrayString[2] = "Dog";
        arrayString[3] = "Mouse";
        arrayString[4] = "kitchen";
        Arrays.sort(arrayString);
        String[] arrReverse = new String[arrayString.length];
        for (int i = arrayString.length - 1; i >= 0; i--) {
            arrReverse[arrayString.length - 1 - i] = arrayString[i];

        }
    }
}

回答by Chintan

String arr[]= new String[];
String s;     //input string
int count=0;
for(int i=0;i<=s.length()-k;i++){
                arr[i]=s.substring(i,i+k);  //using substring method
                count++;
            }


           int i=0;
           int b=count;
         while(count>0){
              int j=0; 
            while(j<b){
                 if((arr[i].compareTo(arr[j])>0)){  
                    String temp= arr[i];
                    arr[i]=arr[j];
                    arr[j]=temp;
                  }
            j++;
            } 
            i++; 
            count--;
        }

 for(i=0;i<b;i++)
     System.out.println(arr[i]);