0

I have an interview exercise today:

return A sorted array (case insensitive). A sorted array will be sorted

 * alphabetically by the first 3 characters, then numerically by 
 * the following number and then alphabetically by the remaining characters with
 * spaces above characters.


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

import org.junit.Test;

public class MySort {


    public String[] testSortArray(String[] input){
        // TODO: Sort the array

    }

    @Test
    public void testSort() {
        String[] input = new String[8];
        input[0] = "AIR1";
        input[1] = "AIR20b";
        input[2] = "BIR5A";
        input[3] = "AIR20AB";
        input[4] = "AIR10ab";
        input[5] = "AIR2 A";
        input[6] = "AIR111";
        input[7] = "AIR1Z";

        MySort sortTest = new MySort();
        String[] output = sortTest.testSortArray(input);

        String[] expected = new String[8];
        expected[0] = "AIR1";
        expected[1] = "AIR1Z";
        expected[2] = "AIR2 A";
        expected[3] = "AIR10ab";
        expected[4] = "AIR20AB";
        expected[5] = "AIR20b";
        expected[6] = "AIR111";
        expected[7] = "BIR5A";
        assertEquals(Arrays.asList(output), Arrays.asList(expected));

        for (String item : output) {
            System.out.println(item);
        }
    }
}

I have implemented testSortArray(String[] input as :

public String[] testSortArray(String[] input){

        Collections.sort(Arrays.asList(input), new Comparator<String>() {
            public int compare(String o1, String o2) {
                return extractNumber(o1) - extractNumber(o2);
            }

            int extractNumber(String s) {
                String num = s.replaceAll("\\D", "");
                // return 0 if no digits found
                return num.isEmpty() ? 0 : Integer.parseInt(num);
            }
        });
        return input;
    }

Can you please tell me what's wrong in my code ? thanks

11
  • 1
    We have to guess that you have wrong in your code? Please add more details. Commented Aug 13, 2013 at 1:55
  • do you have an error message? or what order did the array end up in? Commented Aug 13, 2013 at 1:56
  • 4
    You're not writing your sort to the specification. The specification requires you to 1) compare the first three characters, 2) compare the following numerical digits, 3) compare any following characters. You're just tossing away all the non-numeric characters and comparing the resulting integer (if there is one) - basically just step 2. Since you're skipping steps 1 and 3, you're never going to get correct results. Commented Aug 13, 2013 at 1:57
  • the test did not pass Commented Aug 13, 2013 at 1:57
  • 3
    I can only hope the interview is now over - interviews aren't to test whether a stranger on stackoverflow can solve a problem :p Commented Aug 13, 2013 at 2:27

1 Answer 1

3

Your comparison logic obviously doesn't come close to matching your spec. It completely ignores the first three characters, and ignores everything after the digit after the first three characters. Clearly you need to take these things into consideration or you can never match your spec:

    public int compare(String o1, String o2) {
            String s1 = o1.substring(0, 3);
            String s2 = o2.substring(0, 3);
            if(!s1.equals(s2)) {
                return s1.compareTo(s2);
            }
            String[] fields1 = o1.substring(3).split("[^0-9]", 2);
            String[] fields2 = o2.substring(3).split("[^0-9]", 2);
            int i1 = Integer.parseInt(fields1[0]);
            int i2 = Integer.parseInt(fields2[0]);
            if(i1 != i2) {
                return i1 - i2;
            }
            String r1 = "";
            if(fields1.length > 1) {
                r1 = fields1[1];
            }
            String r2 = "";
            if(fields2.length > 1) {
                r2 = fields2[1];
            }
            return r1.compareTo(r2);
        }

This will match your spec. I tested an obtained the following output:

AIR1
AIR1Z
AIR2 A
AIR10ab
AIR20AB
AIR20b
AIR111
BIR5A
Sign up to request clarification or add additional context in comments.

7 Comments

in my code does input array (in return input) is the same as the argument of the method or it was changed after Collections.sort(...); ?
Your compare seems to only look at the first letter after the digits
@Eric: Are you talking to me? I edited the solution to correct my misreading of the OP well before your comment.
@user1034127: What do you expect, of course it modifies as its return type is void.
Yes I was, but I misread your code. Looking again, it'll only hit the case i was seeing when the string contains non-contiguous digits, but that's ill-defined by the original spec anyway. void return is only half of it - you're also relying on Arrays.asList returning a proxy and not a copy.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.