This automator workflow gets it input from the selected text in Xcode, processes it with the help of AppleScript and replaces the selected text with the sorted lines version.
To install it just place it in your home directories library/services and you are done.
From now on whenever you right klick with a bunch of code lines selected you will see at the button the „SortTextLines“ workflow.
Select it and you are done.
Feel free to open it in Automator to enhance it.
Here’s the zip file Automator Program