简体   繁体   中英

Sorting an array of strings in reverse alphabetical order in Java

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.

    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;
    }
}

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]

Follow up from ARS's answer:

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

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

Can you just turn findLargest to findSmallest, like this:

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;
}

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

and then reverse it.

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()); .

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

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

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];

        }
    }
}
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]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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