Optimize our heatmap sorting code.