11

One short and simple question, but I can't find a solution:

'Is it possible to sort the using directives alphabetically?'

Thank you in advance!

8
  • Is that a question about visual studio, if there's a function to short them? Then you should tell us which version you are using. Commented May 21, 2015 at 12:47
  • Yes, you use the "Sort using directives" menu option. (Under Edit, if I remember rightly.) If you've tried that but it hasn't worked, please give more details...
    – Jon Skeet
    Commented May 21, 2015 at 12:48
  • 1
    And where exactly did you try finding a solution? If you google your title ("Order of Using Directives in C# - Alphabetically") then the first result (that is't this SO post) tells you the answer. Without looking I would assume the other results point to similar answers. Next time, when you ask a question like this, try saying "I can't be bothered to look properly" or "I can find the answer, but I want to increase my SO question count and maybe get some rep"
    – musefan
    Commented May 21, 2015 at 12:53
  • The Productivity Power Tools has an option that will remove and sort usings when you save a file.
    – juharr
    Commented May 21, 2015 at 12:54
  • @TimSchmelter: Indeed, but who would search that with quotes anyway...
    – musefan
    Commented May 21, 2015 at 13:12

4 Answers 4

13

Yes, it is. Just right click your directives, select Organize Usings --> Sort Usings

enter image description here

From documentation;

There are two ways to call the operation:

  • Main Menu - On the Edit menu, point to IntelliSense, point to Organize Usings, and then click Sort Usings.

  • Context Menu - Right-click anywhere inside the code editor, point to Organize Usings, and then click Sort Usings.

EDIT: In Visual Studio 2022, the same option called as Remove and Sort Usings which you can also use Ctrl+R, Ctrl+G shortcut for that.

enter image description here

13
  • I don't find it. There is Intellisense but not "organize usings". I'm using the german IDE but there is nothing related to using. Commented May 21, 2015 at 12:55
  • I think it would be useful for the OP if you expand on your answer and explain how you found that documentation... perhaps you are some freak search wizard that knows some special tricks for finding things?
    – musefan
    Commented May 21, 2015 at 12:55
  • @TimSchmelter Weird. I use VS Ultimate 2012 as English IDE and there is an option. Might be reason version (20xx) or Visual Studio edition (Ultimate, Enterprise, Express etc)? I honestly don't know why :/ Commented May 21, 2015 at 13:03
  • 1
    @SonerGönül: I wonder if perhaps using a different "development environment" has different context menus. I always used "C# environment", though I would expect VS to work out context menus based on file types anyway so...
    – musefan
    Commented May 21, 2015 at 13:11
  • 1
    @musefan Hmm, what you say is totally make sense to me. Maybe if you choose a different development environment when you installing your Visual Studio, this option might be invisible. Maybe that's why these documents are under the Visual C# IntelliSense page not Visual Basic-Specific IntelliSense or JavaScript IntelliSense pages. Commented May 21, 2015 at 13:23
6

go to Options, search for USING. Select Advanced. Check box, "Place 'System' directives first when sorting using".

Now a click on the right click context menu, Remove and Sort Usings, will order the using statements 'correctly'.

enter image description here

4

enter image description here

(C#) On Visual Studio 2017 code editor right-click menu, it is now named "Remove and Sort Usings", not nested in any command. And have CTRL+R, CTRL+G shortcut out of the box.

2

Yes it is. Context Menu - Right-click anywhere inside the code editor, point to Organize Usings, and then click Sort Usings. https://msdn.microsoft.com/en-us/library/bb514113.aspx

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.