generic distributed sorting code