简体   繁体   English

在String数组中对2个字符串进行排序(Java)

[英]Sorting 2 strings inside a String array (Java)

I have two arrays one for temperatures and one for the cities.If two temperatures are the same the corresponding cities must be shown with alphabetical sort order.For instance 我有两个数组,一个用于温度,一个用于城市。如果两个温度相同,则必须按字母顺序显示相应的城市。

  • 24 Sydney 24悉尼
  • 22 Athens 22雅典
  • 22 Auckland 22奥克兰
  • 20 Barcelona 20巴塞罗那

--->This is the sort code. --->这是排序代码。

public static void sort(String[] x , double[] y) {
    boolean doMore = true;

    while(doMore) {    
        doMore = false;  

        for(int i=0; i< x.length-1; i++) {    
            if(y[i] == y[i+1] && x[i] < x[i+1]) {    
                String temp2 = x[i];
                x[i] = x[i+1];
                x[i+1] = temp2;    
            } else if (y[i] < y[i+1]) {    
                double temp = y[i];    
                y[i] = y[i+1];    
                y[i+1] = temp; 

                String temp2 = x[i];        
                x[i] = x[i+1];    
                x[i+1] = temp2;    
                doMore = true;    
            }//if    
        }//for 2    
    }//while    
}// sort */

The problem is that on the if(y[i] == y[i+1] && x[i] < x[i+1]) i can't just compare 2 stings with the operator > . 问题是在if(y[i] == y[i+1] && x[i] < x[i+1])我不能只将2个字符串与运算符>进行比较。 I can't use any ready sort methods either. 我也不能使用任何现成的排序方法。

Does anyone know how to sort tho strings alphabetical there? 有人知道如何按字母顺序对字符串进行排序吗?

String is Comparable , so you can use s1.compareTo(s2) StringComparable ,因此可以使用s1.compareTo(s2)

See String.compareTo for details; 有关详细信息,请参见String.compareTo it returns something negative, 0 or something positive, depending on whether s1 comes before s2 lexicographically, that is to say whether s1 would stand before s2 in a dictionary. 根据字典上s1是否在s2之前,它返回负数,0或正数,也就是说,在字典中s1是否位于s2之前。

The closest answer to your current implementation is G. Bach's; 与您当前的实现方式最接近的答案是G. Bach。 you should look into his answer for an easy solution. 您应该调查一下他的答案以找到简单的解决方案。

Thinking a little outside the box... 跳出框框思考...

You current implementation has the potential downside that your arrays could get out of sync in respect to the indexes which would end up having temperatures for the wrong cities. 您当前的实现方式存在潜在的不利影响,即您的阵列可能与索引不同步,从而最终导致错误城市的温度升高。 You could try a custom type which encapsulates both fiels and have a list of those. 您可以尝试一个自定义类型,该类型将两个字段封装在一起,并有一个列表。 Something like... 就像是...

public class CityTemperatureInfo implements Comparable<CityTemperatureInfo>
{
    public string cityName;
    public double temperature;

    ...
}

Then your app would use the data as a List<CityTemperatureInfo> . 然后,您的应用程序会将数据用作List<CityTemperatureInfo> This would ensure the city names and temperatures are always at the same place in the list. 这样可以确保城市名称和温度始终位于列表中的同一位置。 And if you make the class comparable, you can implement the int compareTo(CityTemperatureInfo other) method which would allow you to simple call sort on your list. 并且,如果使该类具有可比性,则可以实现int compareTo(CityTemperatureInfo other)方法,该方法将允许您对列表进行简单的调用排序。

usually, it is a good idea to keep related data together to avoid potential problems of the data becoming out of sync. 通常,将相关数据保持在一起是一个好主意,以避免数据不同步的潜在问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM