Added sorting to the legend.