如何按字母顺序排序String数组(不使用的compareTo或Arrays.sort)

问题描述:

我需要按字母顺序组织字符串数组。从理论上讲,每个单词的第一个字母大写(虽然不一定,因为一个人不能永远信任的用户)。我曾尝试 Arrays.sort(),它不会运行程序。我已经尝试使用的compareTo(),而它运行程序,当它到达code的那一段,我得到这个错误:

I need to organize an array of Strings alphabetically. In theory, the first letter of each word is capitalized (although not necessarily, because one can't always trust users). I have tried Arrays.sort() and it won't run the program. I have tried using compareTo(), and while it runs the program, when it gets to that section of code, I get this error:

Exception in thread "main" java.lang.NullPointerException
        at java.lang.String.compareTo(Unknown Source)
    at NameandAge.printNameOrder(NameandAge.java:431)
    at NameandAge.print(NameandAge.java:350)
    at NameandAge.main(NameandAge.java:116)

所有的一切我能找到对这个问题给了我这两个解决方案之一。任何其他建议?

Literally everything I can find on this subject gives me one of those two solutions. Any other suggestions?

(根据记录,在code当前写着:)

(For the record, the code currently reads: )

while(!done)
{
    done=true;   
    for(int i=0;i<organizedNames.length-1;i++)
    {
        if(!(organizednames[i]==null))
        {
            String name1=organizedNames[i]; String name2=organizedNames[i+1];
            if(name1!=null&&name2!=null)
            {
                int num=name1.compareTo(name2);
                if(num>0)
                { 
                    temp=organizedNames[i]; //temp is a String that was declared earlier
                    organizedNames[i]=organizedNames[i+1];
                    organizedNames[i+1]=temp;
                    done=false 
                }
            }
        }
    }
}

编辑:尝试过检查,以确保NAME 1和NAME不是。现在的工作,但是这是输出:
     乔结果
     比尔结果
     鲍勃结果
     史密斯结果
     罗德尼结果
     詹姆斯结果
     菲利普结果
     莉莲结果
     查理结果
     天使结果
     卡罗尔结果
     诺亚结果
我加code的整个部分现在(减去while循环,当然)。这基本上是精确解,我发现,和第一个给我任何输出。我究竟做错了什么?

Tried checking to make sure name1 and name2 weren't null. It works now, but this is the output: Joe
Bill
Bob
Smith
Rodney
James
Philip
Lillian
Charlie
Angel
Carol
Noah
I added the whole section of code now(minus the while loop, of course). This is basically the exact solution I found, and the first one to give me any output at all. What am I doing wrong?

编辑(再次):这是code调用排序

EDIT (again): This is the code that calls the sort.

String[]organizedNames=new String[names.length];
organizedNames=sortNames(organizedNames);

而code用于排序本身基本上是在下面的答案。

And the code for the sort itself is basically what's in the answer below.

假设你正在运行冒泡排序算法的一些变化,那你有没有消毒你空字符串输入数组,这个问题很可能是 organizedNames [I] 为null。

Assuming you are running some variation of the bubble sort algorithm, and that you have not sanitised you input array for null strings, the problem is likely that organizedNames[i] is null.

如果这是你需要决定是否要删除空项,或在数组的末尾列出他们的情况。如果是后者,之前做一个比较,检查是否名1 == NULL ||名2 == NULL 如果是这样,NUM设置为-1,这将会把所有的空项数组中的一处。

If this is the case you need to decide if you want to remove null items, or list them at the end of the array. If the latter is true, prior to doing a comparison, check if name1 == null || name2 == null if so, set num to -1, this will put all the null items in an array in one place.

要回答你的第二个问题,试试这个:

To answer your secondary question, try this:

boolean done = false;
while(done == false){
  done = true;
  for(int i=0;i<organizedNames.length-1;i++)
  {
    int num = 0;
    if(organizedNames[i] != null && organizedNames[i + 1] != null)
    {
        String name1=organizedNames[i]; String name2=organizedNames[i+1];
        num=name1.compareTo(name2);
    }
    else if(organizedNames[i] == null && organizedNames[i + 1] == null){
      num = 0;
    }
    else if(organizedNames[i] == null){
      num = 1;
    }
    else {
      num = -1;
    }
    if(num>0)
    {
        String temp=organizedNames[i];
        organizedNames[i]=organizedNames[i+1];
        organizedNames[i+1]=temp;
        done=false;
    }
  }
}