-3

I have two arrays of strings that I am trying to combine into one array. The two arrays are in alphabetical order. I have to combine them into alphabetical order. The way I attempted to do this was to create the combined list as the first list followed by the second list, and then sort them. Unfortunately, my instructor for my class says they want me to combine and sort them in one single step using the compareTo method. Below is my code, how would I accomplish this? The first two arrays are user inputted values in alphabetical order of up to 10,000 words with the rest as null, so for example:

list1 = {"Alfred", "Bev", "Carl", "Dan", null, etc.)

list2 = {"Bob", "Craig", "Dean", "Fran", null, etc.)

list3 goal: {"Alfred", "Bev", "Bob", "Carl", Craig", "Dan", "Dean", "Fran"}

    for (int b = 0; b < list3.length; b++)//adds list1 to merged array 
    {
        if (list1[b] != null) {
            list3[b] = list1[b];
            f++;
        }

    }
    int x = 0;
    for (int y = f; y < list3.length; y++)//adds list2 to merged array
    {
        if (list2[x] != null) {
            list3[y] = list2[x];
            x++;
        }
    }

    for (int q = 0; q < list3.length; q++)//Merged array in alphabetical order
    {
        if (list3[q] != null) {
            for (int b = q; b < list3.length; b++) {
                if (list3[b] != null) {
                    if (list3[q].compareTo(list3[b]) > 0) {
                        String s = list3[q];
                        list3[q] = list3[b];
                        list3[b] = s;

                    }

                }
            }
        }

    }

This is my first time using Stack Exchange so hopefully everything is formatted correctly! Sorry for any errors.

kbz
  • 984
  • 2
  • 12
  • 30
  • The lack of indentation makes your code very difficult to follow. – shmosel Nov 28 '17 at 16:18
  • It looks like you have shown us the old code that appends list 1, appends list 2, then sorts the aggregate list. Where is the code that you have written to "combine and sort them in one single step"? – jarmod Nov 28 '17 at 16:22
  • Welcome to StackOverflow, if a answer/question helped you upvote it, if a answer solved your question, mark it as accepted. – Marcos Vasconcelos Nov 28 '17 at 16:34
  • Downvoted for lack of prior research and lazy formatting. Hint: that preview window exists for a reason. And then: names matter. s, q, b, ... are names that mean exactly nothing. Please understand that code is written to be read by **humans**. – GhostCat Nov 28 '17 at 16:36
  • I haven't written code to combine and sort them because that's what I'm asking. I searched under array-merge and sorting, but the only similar question was about combing two int arrays, which did not use compareTo. Can you link to the question I'm duplicating? And again, sorry for the formatting... I was confused on how to get the code block and indent. – Marie Ohlinger Nov 28 '17 at 16:55
  • @MarieOhlinger If you want people to write the entirety of the code for you, you're on the wrong site. Do your best to figure out how you can adapt the linked question's answer to your use case -- look through documentation, for example. It's hard to start out with, but very, very much worth the effort when you get good at it. (I'm assuming you're a beginner for no particular reason; if you're not, what I said applies two times as much) – Nic Nov 28 '17 at 23:28

2 Answers2

0

If you want to merge and sort at the same time, you may implement InsertionSort in an a class that holds a String array (that you will be increasing in size as you add a new one or start it with the final lenght).

Your steps are like:

start with a array of lenght = list1.lenght + list2.lenght (called result here)
for each value on list1 and list2
compare if its lesser of the first element on the result array
if it is, add it before it and swap all list to the next position
if it is not, compare to the second and insert as its place if lesser and so on

For merge then sort:

Ssorting use Arrays.sort(String[])

And also, you can copy the contents of the arrays with System.arraycopy method.

Your code can be as simple as:

String[] list3 = new String[list1.lenght + list2.lenght];
System.arraycopy(list1, 0, list3, 0, list1.lenght);
System.arraycopy(list2, 0, list3, list1.lenght, list2.lenght);
Arrays.sort(list3);

Here list3 has the result you want.

Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167
0

Providing you can use collections, something like this would do the trick. If you can't use collections then let me know and I will provide an alternative solution.

    String[] array1 = new String[] { "Alfred", "Bev", "Carl", "Dan" };
    String[] array2 = new String[] { "Bob", "Craig", "Dean", "Fran" };

    List<String> list1 = Arrays.asList(array1);
    List<String> list2 = Arrays.asList(array2);

    List<String> combined = new ArrayList<String>();
    combined.addAll(list1);
    combined.addAll(list2);

    System.out.println("Unsorted list: "+ combined.toString());

    Collections.sort(combined);

    System.out.println("Sorted list: "+ combined.toString());

Output:

Unsorted list: [Alfred, Bev, Carl, Dan, Bob, Craig, Dean, Fran] 

Sorted list: [Alfred, Bev, Bob, Carl, Craig, Dan, Dean, Fran]
kbz
  • 984
  • 2
  • 12
  • 30