55

Currently VS has a very useful feature: sort usings (C#).

I want the same functionality for any random text, for example - XML nodes in config files.

How complex to implement that? VS addin, right? Is it possible to call some VS API which is used for sorting usings?

abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • 2
    Quick and dirty: copy into Notepad++, select Edit > Line Operations, click the way you would like to sort and then copy back into Visual Studio – user1007074 Jun 29 '20 at 17:47

7 Answers7

39

VS 2019 & VS 2022:

  1. Select lines. The last selected line is where the cursor is - the line is taken into account even if it is empty and there is no visible selection.
  2. Shift + Alt + L, Shift + Alt + S => Ascending sort.
  3. Shift + Alt + L, Shift + Alt + S (same selection same keys) => Descending sort.

The command can also be invoked from menu (@sharpener): Edit | Advanced | Sort Lines

This shortcut applies to the most used scheme: the Visual C++ 6 keyboard mapping scheme (and possibly other schemes). You can check your scheme in Options | Keyboard | Apply the following additional keyboard mapping scheme:.

You can check the shortcut in Options | Keyboard | Show commands containing: Edit.SortLines | Shortcuts for the selected command:.

zdf
  • 4,382
  • 3
  • 18
  • 29
  • 3
    Or: `Edit | Advanced | Sort Lines` if the shortcuts are assigned to something else... – sharpener Jun 08 '21 at 09:17
  • 4
    More upvotes for this, it's built into VS now, this is what most people will be looking for! (Thankfully this answer is displayed *within* google.) – Issung Jul 05 '21 at 02:55
  • @zdf - now if you knew which commands this represented in VS, that would be even handier, as we could make our own toolbar button for it. – PKD Jan 31 '22 at 21:43
  • 1
    If you want to override the keyboard shortcut; In Visual Studio 2019/2022 this can be found under the keyboard commands key `Edit.SortLines`. Same keybinding sorts asc/desc depending on current selection text. – Aaron Apr 04 '22 at 03:56
  • I don't understand the " (same selection same keys) => Descending sort." part, I tried to do it twice thinking it will change sort direction. But it did not. Could anyone explain ? Thanks very much. – EBDS Mar 20 '23 at 03:07
  • @EBDS Either you're doing something wrong or you have different command bindings. To check the bindings, go to `Tools | Customize | Keyboard... | Show commands containing: Edit.SortLines`. And check `Shortcuts for selected command:`. – zdf Mar 20 '23 at 09:02
26

A nice AddOn for Visual Studio is Code Maid

You select some lines and chose from Context Menu "Sort Lines"

enter image description here

And voilá, your lines are sorted nicely in alphabetical order:

enter image description here

Knasterbax
  • 7,895
  • 1
  • 29
  • 23
  • This doesn't work for Code Maid 10.2 on VisualStudio 2013 when editing a C++ source file. The selected lines are left unchanged. – Eponymous Feb 01 '17 at 21:29
24

Edit: Note that this solution does not work on VS2013 or higher, since support for macros was removed.

You don't necessarily need to code a VS addin to do this: Visual Studio has macros built in. To get started, use Tools, Macros, Record Temporary Macro.

Here's a 'Sort Lines' command I hacked together based on the code that Record Temporary Macro gave me:

Imports System
Imports EnvDTE

Public Module TimModule
    Sub SortLines()
        Dim Selection As TextSelection = DTE.ActiveDocument.Selection
        Dim Lines() As String = Selection.Text.Replace(Environment.NewLine, Chr(13)).Split(Chr(13))
        Array.Sort(Lines)
        DTE.UndoContext.Open("Sort Lines")
        ' Edit - see comments
        ' Selection.Text = String.Join(Environment.NewLine, Lines)
        Selection.Delete
        Selection.Insert(String.Join(Environment.NewLine, Lines)) 
        DTE.UndoContext.Close()
    End Sub
End Module
Tim Robinson
  • 53,480
  • 10
  • 121
  • 138
  • 6
    You are using TextSelection.Text property to change the text. This is usually a reason of a very slow execution. It may take even several seconds. The better way is to use TextSelection.Insert and Delete methods. I haven't tested it but I recommend to replace line: Selection.Text = String.Join(Environment.NewLine, Lines) with 2 lines: Selection.Delete Selection.Insert(String.Join(Environment.NewLine, Lines)) Then you can place your macro on toolbar or menu (http://www.helixoft.com/blog/archives/7) or assign key shortcut to it (http://www.helixoft.com/blog/archives/8) – Peter Macej Jul 28 '10 at 10:45
  • 2
    @Peter, thanks. I didn't spend long writing this - if you add an answer to the OP I'll vote it up – Tim Robinson Jul 28 '10 at 10:51
  • Peter's suggestion changed my running time from ~30 seconds to nearly instantaneous, and also stopped some garbage-lines from being generated. – Michael Paulukonis Sep 27 '11 at 13:47
  • Very helpfull, I would suggest slight modification to avoid empty line from the end of the selection mixing with the result and strange behaviour when only part of one line is selected: Dim count As Integer = Lines.Length If Lines.GetValue(count - 1) = "" Then count = count - 1 If count < 2 Then Exit Sub Array.Sort(Lines, 0, count) – Pavel Machyniak Sep 19 '12 at 11:00
  • This does not work for me: when I debug this macro, the lines are not split — the length of Lines array is 1. – Anton Daneyko Jan 12 '13 at 19:59
  • Ok, I am on the track: my document uses uses LF for line endings (aka '\n'). So the Environment.NewLine is not the same as my line separators. Is there a way to access a line separator on a per file basis? When I open the files with inconsistent line endings VS asks if I want to normalize them, so I assume, there might be a property of a currently opened document, that holds a line separator. Does anyone now how to find it? – Anton Daneyko Jan 12 '13 at 20:35
  • My current workaround is to use Chr(10) instead of Environment.NewLine. – Anton Daneyko Jan 12 '13 at 21:10
  • 9
    VS 2012 has dropped support for macros. – Doug Domeny Aug 08 '13 at 14:11
14

Just found a good free addon: Menees VS Tools 2012 (or 2010 ver) - does exactly that and a few more text tricks. There was a few minor negatives when I installed it, but after leaving a comment on the review page it got fixed within days. Waay to go! =)

There is a 2017 version now: Menees VS Tools 2017

Yuri Astrakhan
  • 8,808
  • 6
  • 63
  • 97
7

This comes up at the top of searches still so I'll add this latest solution. If you are running a current VS Code (and why wouldn't you?) you can use the built-in sorter by hitting ctrl-shift-p (Mac is cmd-shift-p) and type "sort" in the subsequent search box. There are many options.

JamesIsIn
  • 181
  • 1
  • 6
6

You can copy the code into Sublime Text, select the section of code and hit F9 (or go to Edit > Sort Lines). You can then copy it back into Visual Studio.

William
  • 3,335
  • 9
  • 42
  • 74
5

I am personally using the Web Essentials extension by Mads Kristensen. You just select the lines you want to sort and Alt+3 or Alt+4 (asc/desc).