61

Typical scenario: a class that a lot of people have worked on. I'd like to sort methods, properties, etc... in alphabetical order.

I'd like to be able to do this within the region or globally in the class.

I see the feature in Resharper to do it, but it does not seem to do anything.

AngryHacker
  • 59,598
  • 102
  • 325
  • 594

8 Answers8

98

Use the "Cleanup Code" functionality.

The order of the members can be set up in the ReSharper options in Languages, C#, Type Members Layout. This is a well documented XML layout specification which ReSharper uses when reordering members.

Lucero
  • 59,176
  • 9
  • 122
  • 152
  • 10
    @Kyle, if you tell R# to reorder members and your layout specification is correct, it does indeed work. See the R# docs for details; some types (such as unit test classes and interop structs with sequential layout attributes) are not reothered though because the order may be meaningful. – Lucero Dec 29 '11 at 16:58
  • 1
    I don't see anything about sorting alphabetically in the R# spec. – reustmd Jan 17 '13 at 14:22
  • 5
    @manu08, see the [JetBrains help on the topic](http://www.jetbrains.com/resharper/webhelp/Code_Cleanup__Usage_Scenarios__Reordering_Type_Members.html) - you can add a `` to sort by name in the reorder config XML. – Lucero Jan 17 '13 at 18:38
  • 4
    For the benefit of anyone else wondering why their interface-implementing properties are not reordering - there is a separate config element for interface-implemention which was not ordered for me by default. – ultra909 Jul 15 '15 at 07:59
  • 1
    Should look down at jgauffin's post below, sorting isn't activated by default. – Cody Aug 09 '17 at 16:36
64

Sorting is not activated by default. You can activate it by opening the resharper options and then go here:

enter image description here

jgauffin
  • 99,844
  • 45
  • 235
  • 372
  • it looks like there is no option to reorder classes in the same file by name ... do you know if thats possible? – montelof Jul 18 '16 at 20:40
  • 8
    I don't know. I never place multiple classes in the same file. – jgauffin Jul 18 '16 at 21:05
  • 2
    FYI - After changing the options, it still didn't work. I had to restart VS, then it worked. – John MacIntyre Jan 12 '17 at 22:51
  • 1
    I also had to change the "Interface Implementation" option to sort by name, then by none. After a restart of VS it FINALLY worked. So, in short, I went through ALL the options in the patterns and set each to sort by name :D – Johan Danforth May 11 '17 at 08:56
  • 1
    interface implementation members are sorted by the order in the interface. so if you first resort the interface and then the class both will get right. – jgauffin May 11 '17 at 08:58
  • @JohanDanforth Interface implementation is structured as in the interface so that all classes that implement the same interface have those members in the same order. Sort the interface first and then all classes will get the new order when you resort them. – jgauffin Apr 17 '19 at 06:14
  • Thanks. It was exactly I was looking for. – Vikrant Sep 01 '19 at 23:31
49

For the benefit of people, like me, who landed on this question through a web search but found that the detail of the question wasn't quite what they were expecting, you might like to know that you can move individual members up and down within the file by holding down Ctrl-Alt-Shift and then pressing the up or down arrows.

(Obviously that's not the automated arrangement by alphabetical order being asked for in the body of the question, but it was the answer I was hoping I would find for the question in the title.)

Ian Griffiths
  • 14,302
  • 2
  • 64
  • 88
  • This is helpful information that can be useful to folks stumbling across this question. I found what I was looking for in the original posting with the high voted answer, but your answer provided with a helpful shortcut that can be used on the fly. – dub stylee Jan 08 '15 at 05:41
  • 4
    I would add you can click ctrl+m, ctrl+0 to collapse all methods. You can then move a blocks of methods as described above. – jwize May 11 '16 at 22:02
  • Note that that's ctrl-m-oh rather than ctrl-m-zero (on my machine, anyway). Or Edit > Outlining > Collapse to Definitions – dlf Jul 17 '18 at 20:40
  • The first thing I thought was... AWESOME :-) – Franki1986 Oct 01 '18 at 07:01
5

An alternative to consider is Regionerate. We use and like ReSharper, but Regionerate fits our needs for creating regions and sorting/rearranging members. And it's all customizable, of course.

UPDATE: We've started using ReSharper's Code Cleanup for this instead.

tryman
  • 3,233
  • 1
  • 12
  • 23
TrueWill
  • 25,132
  • 10
  • 101
  • 150
  • we used it for about 2 weeks but then when a proliferation of nested regions infested our code we all throw it away. It is a matter of personal taste of course but make sure you all agree on how it is to be used... – zzzuperfly Sep 18 '09 at 11:17
  • @zzzuperfly: Yes, everyone needs to agree on the standards and you need to tweak the configuration. – TrueWill Sep 18 '09 at 14:31
  • 5
    if you use regions its normally a sign that your classes are too big, big = classes mean that you have too many responsibilities per class – roundcrisis May 21 '10 at 14:22
  • 2
    @Miau: While I believe in the Single Responsibility Principle, it's nice to separate properties/public methods/constructors/private methods/fields into their own regions and alphabetize those. – TrueWill May 21 '10 at 16:56
  • 9
    In general regions are a code smell. Regions segregate and hide code. The same thing can be accomplished with classes and methods. – Chuck Conway May 11 '11 at 19:05
  • @Chuck: I generally agree, which is why I set up VS to not collapse regions. Having standards for the order of members in classes is valuable IMHO. – TrueWill May 11 '11 at 23:22
3

Two things: There is a known (but not heavily documented) condition where pre-compile conditionals (#if DEBUG for example) will stop type member reordering. http://youtrack.jetbrains.com/issue/RSRP-336643#tab=Comments In other words if you have #IF DEBUG then it won't reorder.

I also recently noticed that in ReSharper 8.0.1 (and probably earlier versions) that the button to revert the XML template back to DEFAULT WITH REGIONS doesn't really have any statements to include #REGION grouping. So I took a StyleCop friendly template that includes sorting and added #REGION-ing to each type member. If you select CUSTOM TEMPLATE then paste in this XML it should work.

<Patterns xmlns="urn:shemas-jetbrains-com:member-reordering-patterns">

<!--  Do not reorder COM interfaces  -->
<Pattern>
    <Match>
        <And Weight="100">
            <Kind Is="interface" />
            <HasAttribute CLRName="System.Runtime.InteropServices.InterfaceTypeAttribute" />
        </And>
    </Match>
</Pattern>

<!--  Special formatting of NUnit test fixture  -->
<Pattern RemoveAllRegions="true">
    <Match>
        <And Weight="100">
            <Kind Is="class" />
            <HasAttribute CLRName="NUnit.Framework.TestFixtureAttribute" Inherit="true" />
        </And>
    </Match>

    <!--  Setup/Teardow  -->
    <Entry>
        <Match>
            <And>
                <Kind Is="method" />
                <Or>
                    <HasAttribute CLRName="NUnit.Framework.SetUpAttribute" Inherit="true" />
                    <HasAttribute CLRName="NUnit.Framework.TearDownAttribute" Inherit="true" />
                    <HasAttribute CLRName="NUnit.Framework.FixtureSetUpAttribute" Inherit="true" />
                    <HasAttribute CLRName="NUnit.Framework.FixtureTearDownAttribute" Inherit="true" />
                </Or>
            </And>
        </Match>
    </Entry>
    <!--  All other members  -->
    <Entry />
    <!--  Test methods  -->
    <Entry>
        <Match>
            <And Weight="100">
                <Kind Is="method" />
                <HasAttribute CLRName="NUnit.Framework.TestAttribute" Inherit="false" />
            </And>
        </Match>
        <Sort>
            <Name />
        </Sort>
    </Entry>
</Pattern>

<!--  Default pattern  -->


<Pattern RemoveAllRegions="false">
    <!--  Delegates  -->
    <Entry>
        <Match>
            <And Weight="100">
                <Access Is="public" />
                <Kind Is="delegate" />
            </And>
        </Match>
        <Sort>
            <Access Order="public internal protected-internal protected private" />
            <Name />
        </Sort>
        <Group Region="Delegates" />
    </Entry>


    <!--  Fields and constants  -->
    <Entry>
        <Match>
            <Or>
                <Kind Is="field" />
                <Kind Is="constant" />
            </Or>
        </Match>

        <Sort>
            <Access Order="public internal protected-internal protected private" />
            <Kind Order="constant" />
            <Readonly />
            <Static />
            <Name />
        </Sort>
        <Group Region="Fields" />
    </Entry>

    <!--  Enums  -->
    <Entry>
        <Match>
            <Kind Is="enum" />
        </Match>
        <Sort>
            <Access Order="public internal protected-internal protected private" />
            <Name />
        </Sort>
        <Group Region="Enums" />
    </Entry>

    <!--  Constructors. Place static one first  -->
    <Entry>
        <Match>
            <Kind Is="constructor" />
        </Match>
        <Sort>
            <Static />
            <Access Order="public internal protected-internal protected private" />
        </Sort>
        <Group Region="Constructors" />
    </Entry>

    <!--  Destructors. Place static one first  -->
    <Entry>
        <Match>
            <Kind Is="destructor" />
        </Match>
        <Sort>
            <Static />
            <Access Order="public internal protected-internal protected private" />
        </Sort>
        <Group Region="Destructors" />
    </Entry>


    <!--  Events  -->
    <Entry>
        <Match>
            <Kind Is="event" />
        </Match>

        <Sort>
            <Access Order="public internal protected-internal protected private" />
            <Name />
        </Sort>
        <Group Region="Events" />
    </Entry>

    <!--  Properties  -->
    <Entry>
        <Match>
            <And>
                <Kind Is="property" />
                <Not>
                    <Kind Is="indexer" />
                </Not>
            </And>
        </Match>
        <Sort>
            <Access Order="public internal protected-internal protected private" />
            <Static />
            <Abstract />
            <Virtual />
            <Override />
            <Name />
        </Sort>
        <Group Region="Properties" />
    </Entry>

    <!--  Indexers  -->
    <Entry>
        <Match>
            <Kind Is="indexer" />
        </Match>
        <Sort>
            <Access Order="public internal protected-internal protected private" />
            <Static />
            <Abstract />
            <Virtual />
            <Override />
            <Name />
        </Sort>
        <Group Region="Indexers" />
    </Entry>

    <!--  Methods  -->
    <Entry>
        <Match>
            <And>
                <Or>
                    <Kind Is="method" />
                    <Kind Is="operator" />
                    <HandlesEvent />
                </Or>
                <Not>
                    <Kind Is="destructor" />
                </Not>
            </And>
        </Match>
        <Sort>
            <Access Order="public internal protected-internal protected private" />
            <Static />
            <Abstract />
            <Virtual />
            <Override />
            <Name />
        </Sort>
        <Group Region="Methods" />
    </Entry>

    <!--  all other members  -->
    <Entry />

    <!--  nested types  -->
    <Entry>
        <Match>
            <Kind Is="type" />
        </Match>
        <Sort>
            <Access Order="public internal protected-internal protected private" />
            <Static />
            <Abstract />
            <Virtual />
            <Override />
            <Name />
        </Sort>
        <Group Region="Nested Types" />
    </Entry>
</Pattern>

Clint StLaurent
  • 1,238
  • 12
  • 11
0

If you are reordering parameters on specific methods, you can use the Refactor > Change Signature if your cursor is on a method name. I use the IntelliJ shortcuts, so for me, the command is Ctrl+Shift+R followed by Ctrl+F6.

Refactor context menu

After doing so, a dialog will pop up which allows you the reorder method parameters. It will even refactor any implementations of an interface.

birdamongmen
  • 1,940
  • 1
  • 15
  • 12
0

jgauffin's answer is close, but I found that (with R# 2017) to reorder Properties I needed to click the 'XAML' option in the header of the File Layout dialog and change

<Entry DisplayName="Properties, Indexers">
  <Entry.Match>
    <Or>
      <Kind Is="Property" />
      <Kind Is="Indexer" />
    </Or>
  </Entry.Match>
</Entry>

to

<Entry DisplayName="Properties, Indexers">
  <Entry.Match>
    <Or>
      <Kind Is="Property" />
      <Kind Is="Indexer" />
    </Or>
  </Entry.Match>
  <Entry.SortBy>
    <Name />
  </Entry.SortBy>
</Entry>

The 'Sort By' property was empty and read-only, which makes sense because it's only used for items with the same name (and all properties should be uniquely named)

Robin Bennett
  • 3,192
  • 1
  • 8
  • 18
-1

From Visual Studio menu;

ReSharper > Options > Environment > IntelliSense > Completion Behaviour > Sort Items(Alphabetically)

Doğan Etkin
  • 71
  • 2
  • 7
  • This is the sort order of items that appear in the Intellisense, either By Relevance or Alphabetically – Colin Oct 22 '18 at 14:29