How to switch int sort to String sort?

僤鯓⒐⒋嵵緔 提交于 2020-01-15 12:27:06

问题


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!


回答1:


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
}



回答2:


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.




回答3:


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!




回答4:


Try to use Collections.sort() function

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



回答5:


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




回答6:


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




回答7:


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


来源:https://stackoverflow.com/questions/17030737/how-to-switch-int-sort-to-string-sort

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!