如何排序字符串,以便首先显示带有额外信息的值?

时间:2014-07-15 19:54:45

标签: java arrays sorting

我正在尝试对以下字符串进行排序

1.0.0.0-00000000-00000
2.1.0.0
2.2.0.0
2.3.0.0-00000000-00000

我目前在字符串数组中有这些值。

 String[] arrays = {"1.0.0.0-00000000-00000", "2.1.0.0", "2.2.0.0", "2.3.0.0-00000000-00000"};

我正在尝试输出如果没有" - "然后这些值按排序顺序转到数组的末尾。我想要输出如下:

1.0.0.0-00000000-00000
2.3.0.0-00000000-00000
2.1.0.0
2.2.0.0

我已经尝试了Arrays.sort(arrays),但我不确定如何对此进行排序?

import java.util.Arrays;
import java.util.Comparator;
import java.util.Collections;

public class HelloWorld{

     public static void main(String []args){

       String[] arrays = {"1.0.0.0-00000000-00000", "2.1.0.0", "2.2.0.0", "2.3.0.0-00000000-00000"};
       String[] newArray = new String[arrays.length];

    class CustomComparator implements Comparator<String>
    {
        @Override
        public int compare(String a, String b)
        {
            if(a.contains("-") && !b.contains("-"))
                 return 1;
            else if(!a.contains("-") && b.contains("-"))
                 return -1;
            return a.compareTo(b);
        }
    }

    Arrays.sort(arrays, new CustomComparator());

       for(String array : arrays)
       {
            System.out.println(array);
       }

     }
}


Error:

$javac HelloWorld.java 2>&1

HelloWorld.java:25: error: no suitable method found for sort(String[],CustomComparator)
    Collections.sort(arrays, new CustomComparator());
               ^
    method Collections.<T#1>sort(List<T#1>,Comparator<? super T#1>) is not applicable
      (no instance(s) of type variable(s) T#1 exist so that argument type String[] conforms to formal parameter type List<T#1>)
    method Collections.<T#2>sort(List<T#2>) is not applicable
      (cannot instantiate from arguments because actual and formal argument lists differ in length)
  where T#1,T#2 are type-variables:
    T#1 extends Object declared in method <T#1>sort(List<T#1>,Comparator<? super T#1>)
    T#2 extends Comparable<? super T#2> declared in method <T#2>sort(List<T#2>)
1 error



    The method gave me an output of 
2.1.0.0 
2.2.0.0
1.0.0.0-00000000-00000 
2.3.0.0-00000000-00000 

as opposed to

1.0.0.0-00000000-00000
2.3.0.0-00000000-00000
2.1.0.0 
2.2.0.0 

3 个答案:

答案 0 :(得分:4)

使用比较器

    import java.util.Comparator;
    class CustomComparator implements Comparator<String> {
        @Override
        public int compare(String a, String b) {
            if(a.contains("-") && !b.contains("-"))
                 return 1;
            else if(!a.contains("-") && b.contains("-"))
                 return -1;
            return a.compareTo(b);
        }
    }
    Collections.sort(arrays, new CustomComparator());

返回负值表示b出现在a之前,而正值表示a出现在b之前

答案 1 :(得分:1)

较小的值首先出现在较大值之前。将您的Comparator更改为

class CustomComparator implements Comparator<String>
{
    @Override
    public int compare(String a, String b)
    {
        if(a.contains("-") && !b.contains("-"))
             return -1;
        else if(!a.contains("-") && b.contains("-"))
             return 1;
        return a.compareTo(b);
    }
}

答案 2 :(得分:1)

 int dashed = 0;    
//How many strings with a "-" are there?    
for (String s : arrays)    
    if ( s.contains("-") )    
        ++dashed;    

int undashed = arrays.length - dashed;
dashed = 0;
Arrays.sort(arrays);    

for (String s : arrays)    
     if ( s.contains("-") )    
        newArray[dashed++] = s;    
    else    
        newArray[undashed++] = s;  

它很简单,无论数组的大小,字符串还是额外的信息都会起作用。只需为Arrays.sort导入java.util.Arrays。当简单化时,没有理由做不必要的事情。