Link to home
Start Free TrialLog in
Avatar of jaggernat
jaggernat

asked on

display elements from List in descending order

hi guys

I have an arraylist 'list' which has 100 items. i am displaying this list in a table. my requirment is
to display the elements in descending order in group of 10. Any help greatly greatly appreciated.

I want to display the items in desceading order like this
100 th item
99 th item
98th item
97th item
96th item
95th item
94th item
93th item
92th item
91th item
90th item
break; // after first 10 items i want to break off from the loop, at this point assign a temporary variable i whic will be 90.
now when the pagination button is clicked again, the count starts from 90 and goes until 80 and
elements displayed are
89
88
87
...
..80
and when clicked again the next 10 items and so on till the first item



any idea what the 'for loop' should be to display all the elements in descending order  in groups of 10
thanks   very much,
Jay
Avatar of _agx_
_agx_
Flag of United States of America image

One way is create a method that displays x items at a time. The method would return the start number - x (ie. items displayed)

      public int showItemsDesc(List l, int start, int displayItems) {
         ....
      }

Then call it in a loop

            int displayItems = 10;
            int numberOfGroups = list.size() / displayItems;
            int startNum = 100;
            for (int i = 0; i < numberOfGroups; i++) {
                  startNum = showItemsDesc(list, startNum, displayItems);
            }
Avatar of jaggernat
jaggernat

ASKER

actually the arraylist can have any number of items, not just 100, so i wouldnt hardcode
int startNum = 100;

thanks
Just change that value to to the list size

 int startNum = list.size();
ok
but we already have  int numberOfGroups = list.size() ;

can you tell me what code should go inside the method
public int showItemsDesc(List l, int start, int displayItems) {
         ....
      }

thanks
List tenItems = myItems.subList(start, start + 10);
ok, but will  subList(start, start + 10); display in descending order

thanks guys
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial