6

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;
    }
}
3
  • 2
    Are you allowed to use Collections and ArrayList? Commented Dec 8, 2012 at 17:01
  • 1
    Just a small comment: Arrays in Java start with the index 0, not 1. Commented Dec 8, 2012 at 17:01
  • @str OP doesn't have to check if arr[0] > arr[0]; starting the check at index 1 is fine. Commented Dec 8, 2012 at 17:03

8 Answers 8

8

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]
Sign up to request clarification or add additional context in comments.

2 Comments

This seems the most elegant, but I note that the code wants to compare Strings ignoring case. So instead of Collections.reverseOrder() you'd need a custom Comparator.
@bowmore you could use Collections.reverseOrder(String.CASE_INSENSITIVE_ORDER) for that, no need to implement a custom one.
4

Follow up from A. R. S.'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
            }
        });

Comments

2

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

Comments

2

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

Comments

1

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

and then reverse it.

2 Comments

(Which is the opposite of the task.)
Use the two-argument Arrays.sort and pass Collections.reverseOrder() as the comparator.
1
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());.

Comments

1

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

        }
    }
}

Comments

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

1 Comment

this is for generating substrings from a given string and then sorting them in decending order for loop can be changed to get your own substrings

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.