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++;
}