I'd like to sort a file based on the results of a regex expression. For example, if I have the following property declarations in Obj-C
@property (nonatomic, strong) id <AlbumArtDelegate, UITextFieldDelegate> *albumArtView; // 1
@property (nonatomic, strong, readonly) UIImageView *profileView; // 2
@property (nonatomic, strong, readwrite) UIButton *postFB; // 3
@property (nonatomic, assign) UIButton *saveButton; // 4
By default they will sort in order [4, 1, 2, 3], but I would like to sort them in order of the actual property names, [1, 3, 2, 4]. I can write a regular expression to tease out just the property name, is it possible for me to sort by the results of that expression?
Is there any built-in Unix tool that will do this for me? I'm working in Xcode, so VIM/emacs solutions won't help.
Also, the reason I'd like to do this using a regex is so that I can expand my sorting algorithm to work in other situations. Use it to sort method declarations, import statements, etc.
Best Answer
A general method to sort by an arbitrary function of the contents of the line is as follows:
Here is a key you can use in this particular case: this
sed
program will output the the line from the last identifier to the end.To put these keys and the original lines side by side:
To sort them ...
and to leave just the second field (the original line)
All together (sorting in reverse order, so there's something to show):
→