1

What would be the easiest / quickest way to zip a list of strings which is similar to this:

john,barry,stewart,josh,30,45,23,56

I want the order to be

john,30,barry,45,stewart,23,josh,56

I know that sounds like homework but the actual list is used in some selenium code and stores urls and the option of a radio button on the page but I thought the above example is simpler to read.

tucuxi
  • 17,561
  • 2
  • 43
  • 74
Doctor Who
  • 1,287
  • 5
  • 25
  • 46
  • What is the rule of this sorting behavior? This order looks completely arbitrary. – JB Nizet Nov 26 '13 at 09:33
  • Can you describe the idea behind this sorting? Why does `john` come before `30`, which comes before `barry`? This looks more like zipping two lists into one. –  Nov 26 '13 at 09:33
  • Maybe I should have used the real selenium example :P The list I am returning in a method has a bunch of URL which it gets from a page then further in the method I go to those urls and store true or false depending on if a radio button is clicked or not. I then have another method to set everything back to how it was before my test so it would be easier if my list was in order so I would have a url with the value of the radio button next in the list – Doctor Who Nov 26 '13 at 09:40
  • 1
    I still do not get what the sorting criteria is... Try to understand your problem first before trying to ask others, if you understand better than repeating an example, please rephrase the question.... If I look at it, it doesn't even look like sorting rather than just rearranging with one string, one number in the order of their respective group appearances. – Buddha Nov 26 '13 at 10:14

8 Answers8

6

I would do something similar to this:

String[] parts = input.split(",");
int halfLength = parts.length / 2;
for (int i = 0; i < halfLength; i++) {
    String name = parts[i];
    String age = parts[i + halfLength];
    // Store them in whatever structure you want here
}
BambooleanLogic
  • 7,530
  • 3
  • 30
  • 56
  • +1 for simplest idea... I was thinking on the same lines when I saw yours, no point to write another answer after this, assuming that this is what the questioner has intended to do. – Buddha Nov 26 '13 at 10:19
  • Thank you this is the sort of solution I have added. – Doctor Who Nov 26 '13 at 10:57
2

What you need here is a zip function like in functional languages.

Functional java may help you out.

Related: Is there an accepted Java equivalent to Python's zip()?

Community
  • 1
  • 1
Adam Arold
  • 29,285
  • 22
  • 112
  • 207
1

Your question sounds like you need code to fix a bad design. Therefore, I would go a step back and ask the question how the list

john,barry,stewart,josh,30,45,23,56

was created, when you obviously need:

(john, 30), (barry, 45), (stewart, 23), (josh, 56)

(Replace the (x,y) notation with you favorite tuple type.)

Ingo
  • 36,037
  • 5
  • 53
  • 100
0

Here you need to do some manual code.

Firstly scatter all the string and integer separately and obtain two list.

then just iterate one list and add these two list in one final list like first string and

then integer.

shreyansh jogi
  • 2,082
  • 12
  • 20
0

It seems as if you are just trying to interleave the numbers and the alphanumerics. I would just scan the list in order to find the index of the first number than create a new list and take the first alpha and the first number and add them to the new list, then the second alpha and second number. This is not sorting and so is O(n)

Yaneeve
  • 4,751
  • 10
  • 49
  • 87
0

Looks like two lists

First generate two lists and create a new result list

Pseudocode :

list = Arrays.AsList(string.split("\\s+"))

list1 = list.sublist(0, list.size() /2 );
list2 = list.sublist(list.size() /2 +1 , list.size());

for(int i = 0;i<list1.size() ; i++ ) {
   resultlist.add(list1.get(i);
   resultlist.add(list2.get(i);
}
return resultlist
urbiwanus
  • 713
  • 5
  • 21
0

Try this way : 1. First sort it - the numbers will cover half the list and names half the list. 2. Create two arrays half the length : sArr1 and sArr2. Store numbers in sArr1 and names in sArr2. 3. Merge them by putting alternatively the number and the name from the two arrays into original array.

import java.util.Arrays;


public class SortMix {
    public static void main(String[] args) {
        String sArr[] = {"john","barry","stewart","josh","30","45","23","56"};
        Arrays.sort(sArr);
        display(sArr);

        String sArr1[] = new String[sArr.length/2];
        for(int i=0;i<sArr.length/2;i++) 
            sArr1[i] = sArr[i];
        display(sArr1);

        String sArr2[] = new String[sArr.length/2];
        for(int i=0;i<sArr.length/2;i++)
            sArr2[i] = sArr[i+sArr.length/2];
        display(sArr2);

        int k=0;
        int l=0;
        for(int i=0;i<sArr.length;i++) {
            if(i%2==0){ 
                sArr[i]=sArr1[k];
                k++;    
            } else {
                sArr[i]=sArr2[l];
                l++;
            }
        }
        display(sArr);
    }

    public static void display(String[] sArr) {
        for(int i=0;i<sArr.length;i++) 
            System.out.print(sArr[i] + " ");
        System.out.println();
    }
}

Output would be :

23 30 45 56 barry john josh stewart 
23 30 45 56 
barry john josh stewart 
23 barry 30 john 45 josh 56 stewart
Nishant Lakhara
  • 2,295
  • 4
  • 23
  • 46
0

Generic version of zip:

@SafeVarargs
public static <T> ArrayList<T> zip(Iterable<T>  ...iterables) {
    ArrayList<Iterator<T>> is = new ArrayList<>(iterables.length);
    for (int i=0; i<iterables.length; i++) {
        is.add(iterables[i].iterator());
    }
    ArrayList<T> al = new ArrayList<>();
    while (is.get(0).hasNext()) {
        for (int i=0; i<is.size(); i++) {
            // FIXME: could check for shorter-than-expected sublists here
            al.add(is.get(i).next());
        }
    }
    return al;
}

Can be tested / called as follows:

public static void main(String[] args) {
    List<String> parts = 
            Arrays.asList("john,barry,stewart,josh,30,45,23,56".split(","));
    List<String> names = parts.subList(0, parts.size()/2);
    List<String> numbers = parts.subList(parts.size()/2, parts.size());
    System.err.println(zip(names, numbers));
}
tucuxi
  • 17,561
  • 2
  • 43
  • 74