tags:

views:

54

answers:

2

Trying to figure out an equation to get the current group a page would be in if they were grouped like below.

Variables:

PageSize = 5
PageIndex = 21
GroupSize = 5
TotalItems = 1000
CurrentPage = PageIndex + 1

Find:

**CurrentGroup = ?**

If there are 1000 items and you have a group size of 5 then there are 200 groups (TotalItems / GroupSize). This means that CurrentPage 22 must land in group 5

Group 1: 1 2 3 4 5
Group 2: 6 7 8 9 10
Group 3: 11 12 13 14 15
Group 4: 16 17 18 19 20
Group 5: 21 22 23 24 25
+1  A: 

I think this is what you were asking --

Assumes integer math:

 page = ( item / ItemsPerPage ) + 1; // depends if 0 based.
 pageIndex = item % ItemsPerPage;
 group = ( page / GroupSize );

Page is the page you're seeking

pageIndex is it's position on the destination page

group is the actual group it fits in based on GroupSize

In essence, you're just adding another level of paging so the same math works as paging.

Borzio
+2  A: 

Formula

1+floor((CurrentPage-1)/GroupSize)

Test

1 -> 1 
.. 
5 -> 1 
6 -> 2 
.. 
9 -> 2
..
22 -> 5
Ismael
Or, simpler formula: ceiling(CurrentPage/GroupSize). Easy to prove that the two are equivalent.
ShreevatsaR