When you enable taskbar grouping (right click taskbar, choose properties, option "Group similar taskbar buttons") it by default group items when there is very less space available on taskbar.
By default xp group items which were opened first but you can change this default behavior using registry. See following steps
To change this behavior, create a "TaskbarGroupSize" DWORD entry in the following registry key:
The values for this key are:
0 - (Default) Group by age (oldest group first)
1 - Group by size (largest group first)
2 - Group any group of size 2 or more
3 - Group any group of size 3 or more
Log off and then back on for this change to take effect.
Read Original Microsoft Post