Zum zeilenweisen sortieren nehme ich bisher UltraEdit. Kann das eigentlich nicht Visual Studio auch? Nein, dieses Leistungsmerkmal nicht enthalten (@Tim: wie wäre das als ein Feature Request). Es gibt zwar eine Formatierung über Bearbeiten, Erweitert, Dokument formatieren, aber das ist auch schon alles. Extra dann UltraEdit einzusetzen, auch wenn es per Kontextmenü über die in Projektmappen enthaltenen Dokumente leicht geht, ist halt keine smarte Lösung. Die Anforderung, nur den markierten Bereich zu sortieren, bringt dann alles mit den Vorgängen Ausschneiden, Sortieren und Einfügen gleich noch mehr ins Wanken. Das habe ich als Anlass genommen und ein Makro geschrieben.
So sieht eine Datei, beispielweise eine OPML-Datei für einen Blog, unsortiert aus.
Sie ist hier im Beispiel partiell unsortiert und enthält leere Zeilen.

Für eine Sortierung ist einfach der entsprechende Bereich zu markieren.
Die komplette Datei kann per Bearbeiten, Alle auswählen¹ oder STRG + A auch sortiert werden.

Dann ist das Makro SortSelectionAndSmartFormat auszuführen und schon ist alles sortiert und formatiert.
So einfach geht's. Und integriert.

Hier noch das Makro:
#Region "Sub SortSelectionAndSmartFormat"
' Sortiert und formatiert die aktuelle Auswahl.
Sub SortSelectionAndSmartFormat()
If (DTE.ActiveDocument Is Nothing) Then
Exit Sub
End If
If (DTE.ActiveDocument.ReadOnly) Then
Exit Sub
End If
Dim currentTextSelection As TextSelection
currentTextSelection = DTE.ActiveDocument.Selection
If (currentTextSelection.TextRanges.Count = 1) Then
Exit Sub
End If
If (DTE.UndoContext.IsOpen) Then
DTE.UndoContext.Close()
End If
Try
DTE.UndoContext.Open("SortSelectionAndSmartFormat")
currentTextSelection.SmartFormat()
Catch
End Try
Try
Dim currentLines() As System.String = Split(currentTextSelection.Text, vbCrLf)
System.Array.Sort(currentLines)
currentTextSelection.Delete()
For Each i As System.String In currentLines
If Not i = System.String.Empty Then
currentTextSelection.Insert(i.Trim(vbCrLf) & vbCrLf)
End If
Next
DTE.UndoContext.Close()
Catch
End Try
Exit Sub
End Sub
#End Region
#Region "Sub SortSelectionAndSmartFormat"
' Sortiert und formatiert die aktuelle Auswahl.
Sub SortSelectionAndSmartFormat()
If (DTE.ActiveDocument Is Nothing) Then
Exit Sub
End If
If (DTE.ActiveDocument.ReadOnly) Then
Exit Sub
End If
Dim currentTextSelection As TextSelection
currentTextSelection = DTE.ActiveDocument.Selection
If (currentTextSelection.TextRanges.Count = 1) Then
Exit Sub
End If
If (DTE.UndoContext.IsOpen) Then
DTE.UndoContext.Close()
End If
Try
DTE.UndoContext.Open("SortSelectionAndSmartFormat")
currentTextSelection.SmartFormat()
Catch
End Try
Try
Dim currentLines() As System.String = Split(currentTextSelection.Text, vbCrLf)
System.Array.Sort(currentLines)
currentTextSelection.Delete()
For Each i As System.String In currentLines
If Not i = System.String.Empty Then
currentTextSelection.Insert(i.Trim(vbCrLf) & vbCrLf)
End If
Next
DTE.UndoContext.Close()
Catch
End Try
Exit Sub
End Sub
#End Region
Und hier noch das Makro SortSelectionAndSmartFormat (1,21 KB) zum Download.
¹ es sollte wohl richtig heißen "Alles auswählen", da hat wohl jemand das "s" vergessen Tim