46

Similar to emacs' M-x sort-lines

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
Aaron Fi
  • 10,116
  • 13
  • 66
  • 91
  • 12
    Are there any alternatives that do not involve mimicking Emacs? People that have never used Emacs before would also like to sort lines, but are not interested in an Emacs emulator. – Philippe May 31 '11 at 12:30
  • 1
    [Johan Zandin](https://stackoverflow.com/users/15462446) posted an [Answer](https://stackoverflow.com/a/66766761) saying "AnyEdit (mentioned in other answers) have now moved here: [https://github.com/iloveeclipse/plugins/wiki/AnyEdit-Tools](https://github.com/iloveeclipse/plugins/wiki/AnyEdit-Tools)" – Scratte Mar 27 '21 at 09:54

7 Answers7

38

Yes, it does! There is a very simple plugin called SortIt that does exactly what you want. It adds a section in the Edit menu, through which you can do the following:

  • Case Sensitive
  • Case Insensitive
  • Reverse Text
  • Numeric Sort
  • Length Sort

The plugin is source-code agnostic, and it treats whatever you sort as plain text. I find it especially useful when sorting .properties files and setters/getters. For example, using the Case Sensitive sorting I sorted the following code:

msgHeader.setTotalGrossMass(content.getTotalGrossMass());
msgHeader.setReferenceNumber(content.getReferenceNumber());
msgHeader.setTransportModeAtBorder(content.getTransportModeAtBorder());
msgHeader.setCountryOfDestinationCode(content.getCountryOfDestinationCode());
msgHeader.setCountryOfDispatchExportCode(content.getCountryOfDispatch());
msgHeader.setDateOfAmendment(content.getDeclarationDate());                     // TODO: correct?
msgHeader.setIdentityOfMeansOfTransportCrossingBorder(content.getIdentityMeansOfTransportCrossingBorder());
msgHeader.setManualEntryNumber(content.getManualEntryNumber());
msgHeader.setTotalNumberOfPackages(content.getTotalNumberOfPackages());
msgHeader.setAgreedLocationOfGoodsCode(content.getAgreedLocationCode());
msgHeader.setAgreedLocationOfGoodsLNG(content.getAgreedLoclOfGoodsLNG());
msgHeader.setManualEntryYear(content.getManualEntryYear());
msgHeader.setAgreedLocationOfGoods(content.getAgreedLocationCode());
msgHeader.setMethodOfPayment(content.getMethodOfPayment());
msgHeader.setNationalityOfMeansOfTransportCrossingBorder(content.getNationalityMeansOfTransportCrossingBorder());
msgHeader.setAmendmentPlace(content.getDeclarationPlace());                     // TODO: correct?
msgHeader.setAmendmentPlaceLNG(content.getDeclarationPlaceLNG());               // TODO: correct?
msgHeader.setTotalNumberOfItems(content.getTotalNumberOfItems());
msgHeader.setAcceptanceDate(content.getAcceptanceDate());

... to this:

msgHeader.setAcceptanceDate(content.getAcceptanceDate());
msgHeader.setAgreedLocationOfGoods(content.getAgreedLocationCode());
msgHeader.setAgreedLocationOfGoodsCode(content.getAgreedLocationCode());
msgHeader.setAgreedLocationOfGoodsLNG(content.getAgreedLoclOfGoodsLNG());
msgHeader.setAmendmentPlace(content.getDeclarationPlace());                     // TODO: correct?
msgHeader.setAmendmentPlaceLNG(content.getDeclarationPlaceLNG());               // TODO: correct?
msgHeader.setCountryOfDestinationCode(content.getCountryOfDestinationCode());
msgHeader.setCountryOfDispatchExportCode(content.getCountryOfDispatch());
msgHeader.setDateOfAmendment(content.getDeclarationDate());                     // TODO: correct?
msgHeader.setIdentityOfMeansOfTransportCrossingBorder(content.getIdentityMeansOfTransportCrossingBorder());
msgHeader.setManualEntryNumber(content.getManualEntryNumber());
msgHeader.setManualEntryYear(content.getManualEntryYear());
msgHeader.setMethodOfPayment(content.getMethodOfPayment());
msgHeader.setNationalityOfMeansOfTransportCrossingBorder(content.getNationalityMeansOfTransportCrossingBorder());
msgHeader.setReferenceNumber(content.getReferenceNumber());
msgHeader.setTotalGrossMass(content.getTotalGrossMass());
msgHeader.setTotalNumberOfItems(content.getTotalNumberOfItems());
msgHeader.setTotalNumberOfPackages(content.getTotalNumberOfPackages());
msgHeader.setTransportModeAtBorder(content.getTransportModeAtBorder());
Markos Fragkakis
  • 7,499
  • 18
  • 65
  • 103
  • Hrm, I get the error: `Operation is not enabled` when I try to use it. – mlissner Nov 06 '12 at 18:43
  • Hmm, I didn't have a problem in Helios. Why don't you contact the maintainer, in case this is an incompatibility? – Markos Fragkakis Nov 06 '12 at 19:12
  • 4
    +1. btw After adding the update site for SortIt in Eclipse, there will be TWO options to choose from: "State of Flow" and "Uncategorized". To avoid installation errors chose **"State of Flow"**. – informatik01 Feb 26 '14 at 07:38
  • Works like a charm. I'm using it to sort my PHP imports. Couldn't be happier! – NobleUplift Oct 07 '14 at 15:15
  • 1
    Doesn't work for me in Eclipse - Mars. Just does nothing after install and restart from the menus. – Sunny Patel Sep 19 '17 at 20:43
  • 1
    2019-March - SortIt is no longer available from the Eclipse marketplace, nor the links above. It seems to only exist at https://github.com/channingwalton/eclipse-sortit – simpleuser Mar 14 '19 at 16:39
  • 5
    SortIt is dead. Use AnyEdit instead! It works great. See the other answer here: https://stackoverflow.com/questions/3632206/does-eclipse-have-a-way-to-alphabetically-sort-lines-within-a-selection-of-text/14899278#14899278. – Gabriel Staples Dec 12 '19 at 20:08
18

Horribly ugly website, but AnyEdit does sorting:

  • Case sensitive
  • Case insensitive
  • Numerical
  • Backward
  • Forward
Catskul
  • 17,916
  • 15
  • 84
  • 113
  • 1
    Thanks a lot! :) This plugin should be implemented in one of the next Eclipse-releases! :D – Christopher Stock May 21 '13 at 11:21
  • 1
    AnyEdit works great! More info here: https://marketplace.eclipse.org/content/anyedit-tools. To use after installing: **select a bunch of lines → right click → Sort → Case-Sensitive A-z**. Done! – Gabriel Staples Dec 12 '19 at 20:06
6

With luna you can do this by "Source" -> "Organize Imports"

This works for every type of line, not just imports.

  • 3
    Provide an example of how this works for non-import lines please – nikodaemus Nov 04 '16 at 15:20
  • Select the lines you want to be sorted and activate the "Organize Imports" action. – Rainer Hausdorf Dec 05 '16 at 07:13
  • This doesn't seem to work anymore on later versions of Eclipse. On Eclipse 2021-12 I had a non-Java file and the "Organize Imports" action wasn't even available. But even when I cut-pasted the lines I wanted to sort into a Java file and ran "Organise Imports" on it, it still doesn't work. – Fr Jeremy Krieg Mar 23 '22 at 23:17
  • I can confirm that it still works with eclipse 2022-06. – OBu Aug 23 '22 at 21:07
5

In Eclipse Kepler SR2 I can see an option under Source > Sort Members.. It works.

I also have AnyEdit installed.

Saikat
  • 14,222
  • 20
  • 104
  • 125
4

Try this plugin for Eclipse:

http://marketplace.eclipse.org/content/emacs

If the above one doesn't do the job, try one of these ones:

http://marketplace.eclipse.org/search/apachesolr_search/emacs

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
  • sadness, I'm stuck with Eclipse 3.5 for now. No Marketplace Client (and thus emacs plugin) for me. – Aaron Fi Sep 03 '10 at 00:29
  • 1
    No. You can use it for sure. See the additional details: it works in Eclipse Versions: Ganymede or later. Your Eclipse version is Galileo - see this table: http://en.wikipedia.org/wiki/Eclipse_%28software%29#Releases, that is, you have a newer version of Eclipse than that of Ganymede, so you can use it. – Leniel Maccaferri Sep 03 '10 at 00:32
  • um.... that link only takes me to the Eclipse download page. Says all downloads already have MPC installed. But I'm running 3.5 and I don't have it installed. Am I suppose to manually install this Marketplace Client module myself, then use it to install the Emacs plugin? – Aaron Fi Sep 03 '10 at 00:42
  • 1
    You can access the market place from the Help menu within Eclipse. – Leniel Maccaferri Sep 03 '10 at 00:45
  • Sorry to keep pushing this, but... I'm not seeing it in my help menu. I have "Check for Updates" and "Install New Software", but no "Eclipse Marketplace". I'm running Eclipse Java EE IDE for Web Developers, Build id: 20100218-1602 – Aaron Fi Sep 03 '10 at 00:56
  • 1
    So, OK. I have Eclipse Java EE IDE for Web Developers too (my version is 3.6 Helios and it does have the Market Place menu option). But you can install it anyways. Click the Install New Software menu option. Then copy and paste this URL into the Work with field: http://www.mulgasoft.com/emacsplus/update-site. Click the Add button and give whatever name to it. It should now display the Emacs package. Select it and click next. Voila. – Leniel Maccaferri Sep 03 '10 at 01:06
  • 1
    Awesome, thank you. Sadness that I can only up-vote you once. – Aaron Fi Sep 03 '10 at 01:09
  • hehehe... you're welcome. I'm here to help. I'm glad with your up vote. – Leniel Maccaferri Sep 03 '10 at 01:11
  • um, sorry, last question -- how do I activate the emacs plugin? Do I need to set my keybindings to "emacs" mode? (didn't seem to work) – Aaron Fi Sep 03 '10 at 01:25
2

Try CTRL+ALT+S. Works for me with Kepler.

If it doesn't work, goto Help->Key Assist... and look for "Sort Lines"

EDIT: seems to work only in the C/C++ Editor and therefore probably comes with the CDT

clktmr
  • 73
  • 4
0

If you are under Linux, simply use "cat file | sort". Under Windows, as a programmer, you should probably have cygwin installed anyways.

user1050755
  • 11,218
  • 4
  • 45
  • 56