Kristy Welsh - 11 months ago 43

Java Question

I'm trying to sort an array so that the elements in the array can be printed out to a number of columns to the screen in table Rows. I would like the array to be sorted like this:

`a[0] = "Question 1"`

a[1] = "Question 2"

a[3] = "Question 3"

a[4] = "Question 4"

a[5] = "Question 5"

a[6] = "Question 6"

a[7] = "Question 7"

sorted to:

`a[0] = "Question 1" a[1] = "Question 5"`

a[2] = "Question 2" a[3] = "Question 6"

a[4] = "Question 3" a[5] = "Question 7"

a[6] = "Question 4"

Here is my code so far:

`ArrayList<HolderAnswer> listAnswers = getListAnswers();`

TreeMap<Integer, HolderAnswer> treeMapAnswers = new TreeMap<Integer, HolderAnswer>();

// make higher number of answers on the right

if (listAnswers.size() % NUMBER_OF_COLUMNS > 0)

NumberInColumns++;

int count = 0;

int countOfRows = 0;

// sort by row

for (int k = 0; k < listAnswers.size(); k++) {

for (int j = 0; j < NumberInColumns; j++) {

if (k == 0) {

treeMapAnswers.put((Integer) 0, listAnswers.get(k));

} else {

if (k % NumberInColumns > 0)

treeMapAnswers.put((Integer) j, listAnswers.get(k));

count++;

}

}

count = count + NumberInColumns;

}

}

I am stuck in trying to figure out the logic to do this. Please help.

Here is modified code that still doesn't work:

`ArrayList<HolderAnswer> listAnswers = getAnswers();`

ArrayList<ArrayList<HolderAnswer>> listAnswersSorted = new ArrayList<ArrayList<HolderAnswer>>();

int count = 0;

int k=0;

for (HolderAnswer answer : listAnswers) {

ArrayList<HolderAnswer> temp = new ArrayList<HolderAnswer>();

temp.add(answer);

if (k % NumberInColumns == 0 && k != 0 ) {

listAnswersSorted.add(temp);

}

k++;

}

Answer Source

unless it's serving some other purpose, your code is overcomplicated for what you're doing. You can just iterate through `listAnswers`

directly to populate another list with the new sort.

Think about adding elements from `listAnswers`

to a new list `newList`

in order. First, you want to add the 0th element followed by the 4th, then the 1st followed by the 5th, then the 2nd followed by the 6th, then the 3rd (which would be followed by the 7th, but in your example there is no 7th element).

So we've looped through 4 times, each time adding a pair of elements to the new list (except possibly on the last loop). Notice that each of the pairs of elements are offset by 4, which is equal to size of the bigger half of `listAnswers`

. We can calculate this by taking (size of the list + 1)/2 (try a few examples if you're not convinced).

In code:

```
List<HolderAnswer> newList = new ArrayList<HolderAnswer>();
int loop = 0;
int offset = (listAnswers.size() + 1) / 2;
while (newList.size() < listAnswers.size()) {
newList.add(listAnswers.get(loop);
if (newList.size() < listAnswers.size()) {
newList.add(listAnswers.get(loop + offset);
}
loop += 1;
}
```

EDIT: ok, I see what you mean. Your code looks close, but you need to add the elements consecutively to the `ArrayList<HolderAnswer>`

s in `listAnswersSorted`

. There may be an easier way, but I did this by keeping three variables `index`

, `row`

, and `col`

, which represent the iteration through listAnswers, the row, and the column in the 2D array 'listAnswersSorted', respectively.

```
ArrayList<HolderAnswer> listAnswers = getAnswers();
ArrayList<ArrayList<HolderAnswer>> listAnswersSorted =
new ArrayList<ArrayList<HolderAnswer>>();
// initialize the ArrayLists in listAnswersSorted
int numRows = listAnswers.size() / numColumns + 1;
for (int i = 0; i < numRows; i += 1) {
listAnswersSorted.add(new ArrayList<HolderAnswer>());
}
// calculate column index where the "step" happens
int step = listAnswers.size() % numColumns;
// loop through and add elements to listAnswersSorted
int index = 0;
int row = 0;
int col = 0;
while (index < listAnswers.size()) {
listAnswersSorted.get(row).add(listAnswers.get(index));
int rows = col < step ? numRows : numRows - 1;
row += 1;
if (row == rows) {
row = 0;
col += 1;
}
index += 1;
}
// flatten the ArrayList<ArrayList> into a single ArrayList
ArrayList<HolderAnswer> newList = new ArrayList<HolderAnswer>();
for (ArrayList<HolderAnswer> list : listAnswersSorted) {
newList.addAll(list);
}
```