1

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++;
    }
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Kristy Welsh
  • 7,828
  • 12
  • 64
  • 106

2 Answers2

1

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);
}
Patricia Li
  • 1,346
  • 10
  • 19
  • What about a case where there are 3 columns? Your code wouldn't work for that. – Kristy Welsh Mar 11 '14 at 13:41
  • no, it wouldn't. In your example you only displayed two columns, and in your code it's unclear what `NumberInColumns` means. Can you be more specific about what you're trying to do? – Patricia Li Mar 11 '14 at 18:16
  • I am trying to print out RadioButtons as a class I've extended from a table layout. I'm trying to get it as customizable as possible so I don't want to be married to 2 columns. I modified my original answer to show the (bad) progress I've made. – Kristy Welsh Mar 11 '14 at 23:09
  • I just updated my answer to reflect your clarifications. Let me know if that helps. – Patricia Li Mar 12 '14 at 00:44
  • Here's where I implemented it: http://stackoverflow.com/questions/10425569/radiogroup-with-two-columns-which-have-ten-radiobuttons/22465700#22465700 – Kristy Welsh Mar 18 '14 at 14:15
0

I get the feeling you are going about it the "wrong" way. If you are working with TableLayout, then you don't need to think ahead of how TableLayout works internally. Just sort the array how you want (desc/asc etc) and build your table after that. You define your table with how many columns and then all you do is add rows.

In your sorted example you have two columns. Define 2 columns (this can be done dynamically, depending on your data) and add rows and the results would be just like you have there.

I'll give a code example:

for (int i = 0; i < listAnswersSorted.size(); i++) {
        TableRow table_row = new TableRow(this);
        TextView tv = new TextView(this);
        table_row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
        table_row.setGravity(Gravity.CENTER_HORIZONTAL);
        tv.setText(listAnswersSorted.get(i));
        table_row.addView(tv);
    }

...

  table.addView(table_row);

Hope you solve the problem in any case.

VonSchnauzer
  • 912
  • 11
  • 17