Does Eclipse have a way to alphabetically sort lines within a selection of text?

Aaron Fi picture Aaron Fi · Sep 3, 2010 · Viewed 22.8k times · Source

Similar to emacs' M-x sort-lines

Answer

Markos Fragkakis picture Markos Fragkakis · Jul 14, 2011

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());