Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort strings such that values with extra information appear first?

I am trying to sort the following strings

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

I currently have these values in an array of strings.

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

I am trying to have an output where if there is no "-" then those values go to the end of my array in a sorted order. I am trying to have an output as follows:

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

I have tried Arrays.sort(arrays) but I am not sure as to how to go about sorting this?

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 
like image 214
user3767481 Avatar asked Jul 15 '14 19:07

user3767481


3 Answers

Use a comparator

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

return a negative value means b comes before a and a positive value means a comes before b

like image 54
Cheruvian Avatar answered Nov 20 '22 16:11

Cheruvian


Smaller values comes first before the greater ones. Change your Comparator as below

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);
    }
}
like image 42
Cahit Gungor Avatar answered Nov 20 '22 17:11

Cahit Gungor


 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;  

It's simple and will work no matter the size of the array, the strings or the extra info. Just import java.util.Arrays for Arrays.sort. No reason to do unnecessary stuff when simplicity is possible.

like image 1
NarekOnLine Avatar answered Nov 20 '22 16:11

NarekOnLine