Efficient sorting during repetitive statistical computations: algorithms and an application