Preserve keys when sorting parameters when generatings urls

Otherwise invalid urls will be generated.

This fixes #8375 for me but I'm not sure if this is the only issue.

cc @tanghus @schiesbn @LukasReschke