java - Trying to sort an array to be displayed in table rows -
i'm trying sort array elements in array can printed out number of columns screen in table rows. array sorted 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 code far:
arraylist<holderanswer> listanswers = getlistanswers(); treemap<integer, holderanswer> treemapanswers = new treemap<integer, holderanswer>(); // make higher number of answers on right if (listanswers.size() % number_of_columns > 0) numberincolumns++; int count = 0; int countofrows = 0; // sort row (int k = 0; k < listanswers.size(); k++) { (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 stuck in trying figure out logic this. please help.
here modified code still doesn't work:
arraylist<holderanswer> listanswers = getanswers(); arraylist<arraylist<holderanswer>> listanswerssorted = new arraylist<arraylist<holderanswer>>(); int count = 0; int k=0; (holderanswer answer : listanswers) { arraylist<holderanswer> temp = new arraylist<holderanswer>(); temp.add(answer); if (k % numberincolumns == 0 && k != 0 ) { listanswerssorted.add(temp); } k++; }
unless it's serving other purpose, code overcomplicated you're doing. can iterate through listanswers directly populate list new sort.
think adding elements listanswers new list newlist in order. first, want add 0th element followed 4th, 1st followed 5th, 2nd followed 6th, 3rd (which followed 7th, in example there no 7th element).
so we've looped through 4 times, each time adding pair of elements new list (except possibly on last loop). notice each of pairs of elements offset 4, equal size of bigger half of listanswers. can calculate taking (size of list + 1)/2 (try 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, see mean. code looks close, need add elements consecutively arraylist<holderanswer>s in listanswerssorted. there may easier way, did keeping 3 variables index, row, , col, represent iteration through listanswers, row, , column in 2d array 'listanswerssorted', respectively.
arraylist<holderanswer> listanswers = getanswers(); arraylist<arraylist<holderanswer>> listanswerssorted = new arraylist<arraylist<holderanswer>>(); // initialize arraylists in listanswerssorted int numrows = listanswers.size() / numcolumns + 1; (int = 0; < numrows; += 1) { listanswerssorted.add(new arraylist<holderanswer>()); } // calculate column index "step" happens int step = listanswers.size() % numcolumns; // loop through , add elements 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 arraylist<arraylist> single arraylist arraylist<holderanswer> newlist = new arraylist<holderanswer>(); (arraylist<holderanswer> list : listanswerssorted) { newlist.addall(list); }
Comments
Post a Comment