-6

Hi I am trying to Write a function that will sort a string array, for example

{"1","112","99", a, e, g}

    String arr[] = { "1", "112", "99", "e", "a", "g" };

    // Arrays.sort(arr);

    int numberArray[] = new int[3];
    String stringArr[] = new String[3];

    for (String value : arr) {
        boolean isNumeric = value.chars().allMatch(x -> Character.isDigit(x));

        if (isNumeric) {
            for (int i = 0; i < numberArray.length; i++) {
                numberArray[i] = Integer.parseInt(value);
            }
        } else {
            for (int i = 0; i < stringArr.length; i++) {
                stringArr[i] = value;
            }
        }

        Arrays.sort(numberArray);
        Arrays.sort(stringArr);

        System.out.println(numberArray);
        System.out.println(stringArr);

    }
}
Pshemo
  • 122,468
  • 25
  • 185
  • 269
  • Sort in what way? You need to explain some more. – RaminS Jan 14 '16 at 21:23
  • The output would be { "1", "99", "112", a , e, g} – Simon Hill Jan 14 '16 at 21:26
  • This is not a question. It is an assignment. – John Sensebe Jan 14 '16 at 21:29
  • In my logic I am trying to separate the numbers and letters into 2 arrays. Then adding them back into a string array. Just having problems adding them to separate arrays. especially after converting the number strings to Int. – Simon Hill Jan 14 '16 at 21:39
  • 1
    Where is your code? We are not code-writing-service but we can help you with problems with code you already written as long as you show it and describe *specific* problem you want to fix. – Pshemo Jan 14 '16 at 21:43
  • updated the question – Simon Hill Jan 14 '16 at 21:48
  • You still need to describe problem you are facing. – Pshemo Jan 14 '16 at 21:49
  • Ok, I see code. Good. I see requirements. Good. What I don't see is: does the code do what you want it to do? What should it do? What does it do? – Erick G. Hagstrom Jan 14 '16 at 21:50
  • I indented your code so you could see what happens when. Was your intention to sort and print your arrays in each iteration? Also are you sure that you need to set every element in `numberArray` or `stringArr` to currently tested? And about `System.out.println(numberArray);` you should read: [What's the simplest way to print a Java array?](http://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array) – Pshemo Jan 14 '16 at 22:01

1 Answers1

0

First of all a, e and g are variables and not necessarily strings. What you mean is probably "a", "e" and "g".

Your question is essentially:

How do I sort the values in a String[] so that numbers are sorted numerically and prioritized before letters (or words?) which are sorted alphabetically?

Your question seems incomplete, though. How would you sort strings like "15qwe" or "asd34"? Since these could be interpreted in different ways and you haven't told us the rules for these I will ignore these and help you with the case of only numbers and only letters.

There is a class called Integer that has a static method called parseInt(). If the argument to this method does not contain any integers it will throw an exception, so it is a simple way for you to check if it is a number or letter.

For example:

String[] arr = {"5", "7", "a", "1", "g", "b"};
String[] numberArray = new String[3];
// keep this as a String[] for simpler concatenation of the arrays later

String[] stringArr = new String[3];

/*if you do not know that there will be exactly 3 of each, 
  just write arr.length isntead of 3*/

for(int i = 0; i < arr.length; i++) {
    int numberIndex = 0;
    int stringIndex = 0;
    try {
        Integer.parseInt(arr[i]);
        numberArray[numberIndex] = arr[i];
        numberIndex++;
    } catch (Exception e) {
        String s = arr[i];
        stringArr[stringIndex] = s;
        stringIndex++;
    }
}
Arrays.sort(stringArr);
Arrays.sort(numberArray);

Now all you have to do is to concatenate the two arrays into one.

// you can replace numberArray.length and stringArr.length with 3 if you want
for(int i = 0; i < numberArray.length; i++) {
    arr[i] = numberArray[i];
}
for(int i = 0; i < stringArr.length; i++) {
    arr[numberArray.length + i] = stringArr[i];
}

System.out.println(Arrays.toString(arr));

Mistake found:

Since the numberArray is a String[] it will still sort alphabetically and not numerically. If you for example have "3", "1" and "15", it will sort them in this order: {"1", "15", "3"}. This is probably not what you want, so it was a mistake on my part to have a String[] instead of an int[].

So what you need to do is to implement these changes to the code:

  • int[] numberArray = new int[3]; instead of String[] numberArray = new String[3];

  • numberArray[numberIndex] = Integer.parseInt(arr[i]); instead of: Integer.parseInt(arr[i]); numberArray[numberIndex] = arr[i];

When you are going to concatenate the arrays later, you need to convert the integers to strings again. This is easily done with the static method Integer.toString().

So you need to make this small change to the code as well:

arr[i] = Integer.toString(numberArray[i]); instead of arr[i] = numberArray[i];

RaminS
  • 2,208
  • 4
  • 22
  • 30
  • `boolean isNumeric = value.chars().allMatch(x -> Character.isDigit(x));` is perfectly valid code which will let us avoid this ugly `try-catch` block. – Pshemo Jan 14 '16 at 22:20
  • @Pshemo Sure. I was writing this before the question was edited. My `try-catch`-block can be disregarded. – RaminS Jan 14 '16 at 22:21