1

I have written some code for sorting random integers that a user inputted. How would I switch this into sorting randomly inputted letters? Aka, user inputs j, s, g, w, and the programs outputs g, j, s, w?

for (int i = 0; i < random.length; i++) { //"random" is array with stored integers
                // Assume first value is x
                x = i;
                for (int j = i + 1; j < random.length; j++) {
                    //find smallest value in array (random)
                    if (random[j] < random[x]) {
                        x = j;
                    }
                }
                if (x != i) {
                    //swap the values if not in correct order
                    final int temp = random[i];
                    random[i] = random[x];
                    random[x] = temp;
                }
                itsATextArea.append(random[i] + "\n");// Output ascending order
            }

Originally I hoped (though I knew the chances of me being right were against me) that replacing all the 'int' with 'String' would work...naturally I was wrong and realized perhaps I had to list out what letter came before which by using lists such as list.add("a"); etc.

I apologize if this seems like I am asking you guys to do all the work (which I'm not) but I'm not entirely sure how to start going about this, so if anyone can give some hints or tips, that would be most appreciated!

3
  • So, you want to sort letters or strings of letters? Commented Jun 10, 2013 at 18:50
  • I swear this is not, my school does not offer this class so I'm trying to learn myself...so oddly enough...I wish this was homework Commented Jun 10, 2013 at 18:52
  • 1
    If you can use the standard libraries, you can simply use Arrays.sort(random): docs.oracle.com/javase/7/docs/api/java/util/… Commented Jun 10, 2013 at 18:53

7 Answers 7

5

You could use String.compareTo() to do that:

Change this:

int[] random = new int[sizeyouhad];
...
if (random[j] < random[x]) {
...
final int temp = random[i];

to:

String[] random = new String[sizeyouhad];
...
if (random[j].compareTo(random[x]) < 0) {
...
final String temp = random[i];

Trial with your code:

String[] random = new String[3];
random[0] = "b";
random[1] = "c";
random[2] = "a";
int x = 0;
//"random" is array with stored integers
for (int i = 0; i < random.length; i++) { 
    // Assume first value is x
    x = i;
    for (int j = i + 1; j < random.length; j++) {
        //find smallest value in array (random)
        if (random[j].compareTo(random[x]) < 0) {
            x = j;
        }
    }
    if (x != i) {
        //swap the values if not in correct order
        final String temp = random[i];
        random[i] = random[x];
        random[x] = temp;
    }
    System.out.println(random[i] + "\n");// Output ascending order
}
Sign up to request clarification or add additional context in comments.

4 Comments

This is great! However the program still freaks out and the first error being "Exception in thread AWT ....... java,lang.NullPointException"
Should I be declaring an order of the alphabet before all this...?
You are likely inserting null values on 'random', which will cause you that NullPointerException.
Yes I ending up noticing and was like "oh..." so problem(s) solved and Thank you so much for your help and assistance! =)
0

If you're just trying to sort a list of strings you should probably use the java.util.Collections.sort method rather than writing your own sorting routine.

1 Comment

I think this is a learning exercise more than anything else. Otherwise, yeah, don't reinvent the wheel.
0

Was random originally int[]? If you had changed this to String[], you can use String#compareTo method to discern if one string is "less than" another.

Incidentally, you can change the type of random to Comparable[] and then you can use the same algorithm to sort any object whose class implements the interface!

Comments

0

Try to use Collections.sort() function

List<String> l = Arrays.asList("j","s", "g","w");
Collections.sort(l);

Comments

0

If you consider every character to be a code point[1] and you want to sort by Unicode code point order[2], then there is really no need to change your logic. The work is converting from whatever input you are given (String, char[], etc.) into an int[] of the code points.

[1] - http://docs.oracle.com/javase/6/docs/api/java/lang/String.html#codePointAt(int) [2] - http://en.wikipedia.org/wiki/Code_point

Comments

0

You can make your code work on any type of Object by using generics.

Comments

0

The following code is very simple and works perfectly (With this library you can solve your problem in few lines):

import static ch.lambdaj.Lambda.sort;
import static ch.lambdaj.Lambda.on;
import java.util.Arrays;
import java.util.List;

public class Test{
        public static void main(String[] args) {
            List<String> list =  Arrays.asList("1","102","-50","54","ABS");

            List<String> newList = sort(list, on(String.class));
            System.out.println(newList);//[-50, 1, 102, 54, ABS]


}
}

This code uses lambda library (download here, website). Find in the website this example:

List<Person> sorted = sort(persons, on(Person.class).getAge());

Comments

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.