-4

If i have an array of strings, for example, String[] myS = new String[] {"1234", "abcd", "234", "bcd", "34", "cd"}, will sort work on this? I assumed sort on strings would sort it alphabetically, but if so, would it break once it sees a string containing numerical digits?

Here's my function (which is taking in 2 string arrays and doing a substring comparison of 1 and 2.)

public static String[] inArray(String[] array1, String[] array2) {

     if(array1.length == 0 || array2.length == 0) return new String[]{};

     int size=0, index = 0;
     Boolean flag = false;
     for(int i=0; i<array1.length; i++){
       flag = false;
       for(int j=0; j<array2.length; j++){
          if(array2[j].contains(array1[i])){
            flag = true;
            break;
          }
       }

       if(flag == false) array1[i] = "";
       else size++;
     }

     if(size == 0) return new String[] {};  //No matches found

     String[] sortedArr = new String[size];

     for(int i = 0; i<size; i++){
       if(array1[i] != ""){ 
         sortedArr[index] = array1[i];
         index++;
       }
     }

     Arrays.sort(sortedArr); //Occasionally throws null pointer exception
     return sortedArr;
}
Noobgineer
  • 758
  • 5
  • 10
  • 24

1 Answers1

0

Nope, a "String containing numerical digits" si also a String...

Sorting is generally based on Unicode codepoints, thus numbers would sort before letters.

Usagi Miyamoto
  • 6,196
  • 1
  • 19
  • 33