segregate and rearrange