简体   繁体   中英

How to switch int sort to String sort?

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!

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
}

如果您只是尝试对字符串列表进行排序,则可能应该使用java.util.Collections.sort方法,而不是编写自己的排序例程。

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!

Try to use Collections.sort() function

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

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

您可以使用泛型使代码在任何类型的Object上工作。

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

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