Alphabetizing methods in Visual Studio

32,826

Solution 1

Resharper has a Type Members Layout, which can order members by type, accessibility and alphabetically as well.

You can also take a look into Ora , which presents a pane in visual studio that is ordered (even though your source may not be). Link's dead.

Solution 2

While Resharper has many cool features it has a large impact in CPU and I/O usage and can be very complicated to use. It is also only available under commercial licensing unless you qualify for a few very specific free use licenses.

Try CodeMaid. It is free for commercial use and has a much lower performance overhead. I find it easy to use and it is very good for alphabetizing methods.


To sort your file, open the file via solution explorer:

  1. Right click the open file
  2. Code Maid menu (likely near the top of the right click menu)
  3. Click Reorganize Active Document

Alternatively, using the default CodeMaid hotkeys CTRL+M,Z to sort your active file.

Solution 3

The following answer goes much further than the OP asks, because I believe, that sorting methods just by name is far not enough for most programmers. Mostly you want your methods, constructors, fields, event handlers, and interface implementations to be seperated from each other (i.e. through regions), and sorted by various criteria.

For this purpose I tried NArrange, Regionerate, and others, but I've found them not intuitive to use. So ReSharper became the tool of my choice.

As already mentioned by @DavidN, ReSharper can sort your class members (fields, constructors, methods, delegates) by name, accessibility, type, readonly, etc... You can also surround specific members with regions. What I like the most is the ability to group interface members (e.g., #region IDisposable with void Dispose() method in it) and methods that handle an event.

ReSharper provides both - an easy way to configure and trigger the sorting of class members.

Configuring the layout

Create a XML file within Visual Studio and copy-paste the default type member layout (ReSharper Options > Languages > C# > Type Members Layout) into that file. Download the latest XSD schema. Add the schema file to Visual Studio's schema files (Menu > XML > Schemas... > Add). You should be able now, to edit the XML file with IntelliSense support.

Triggering the reorder

If you use the Visual Studio keyboard scheme (ReSharper Options > Visual Studio Integration) and press Ctrl+E,F for Silent Code Cleanup. A dialog will pop up, where you can select a Code Cleanup setting. For this setting you should check Reorder type members. The second time you press the shortcut, ReSharper will automatically reorder your class members.

1: ReSharper Type Members XSD Schema

Solution 4

For C# you can use Regionerate to organize your code. You can create a template that simply organizes your code but doesn't use regions.

Solution 5

You can create such a function yourself using Devexpress' free DXCore product (the same extensibility framework used to create CodeRush).

Share:
32,826
80bower
Author by

80bower

Always learning.

Updated on July 08, 2022

Comments

  • 80bower
    80bower almost 2 years

    Is there any sort of plug-in or tool available for Visual Studio 2008 to alphabetize methods? Ideally I'd like a tool that will alphabetize a selection, or specified type (i.e. only methods, not member variables), either automatically or on-demand.