Hi, this is Gary with MacMost.com. Let me show you different ways that
you can sort the lines of a text file on your Mac.
MacMost is brought to you thanks to a great group of more than 1000 supporters.
Go to MacMost.com/patreon. There you can read more about the Patreon Campaign.
Join us and get exclusive content and course discounts.
So let's say you have a list that you want to sort alphabetically like maybe a list of names.
So here I've got a text file that is a list of 100 names and I want to sort it. You would think
that there would be some sort of function in TextEdit or some other apps to sort this list.
But there's not. But there are many ways to do it. So let's take a look at some of them.
First, let's select All, Command A and Command C to Copy this list. Let's use
Pages to do it. So you want to create a new blank document here and I'm not going to Paste the list
directly into the body text because I still can't sort it that way in Pages. But I can sort a Table.
So I'm going to insert a Table here, just a plain table. I'm actually going to click here and bring
it down to one column and then I'm going to select the first cell. I'm going to be careful not to
double click and be actually typing in the cell. I just want to select the first cell all by itself
and then Command V to paste. It should paste every name to a different row. Now that I've got that I
can go to the Column Heading here, A. Click there, choose Sort and now they are sorted. I still have
everything selected so I can do Command C to Copy. Now I'm going to Hide Pages and go back to my text
document and now I can replace everything, Command V to Paste it in. If you're in Rich Text Format
you may instead want to use Edit and Paste In that Style so that it doesn't paste the table in.
Now instead you could use a spreadsheet to do this. So for instance we can go into Numbers here
and just create a new document. I could just paste in all of those names here, sort that column
just like I would in Pages. Now with that still selected I could Copy and then I can
Replace everything here. So it's easy to take your list into Pages or Numbers,
Sort, and then Copy and Paste it back where you want it.
What about editing this text file directly. Well, you can do that using Terminal because
command line apps are pretty good at handling things like alphabetizing. So let's launch
Terminal. So now to Sort this file let's go to where it's located. So I'm going to do CD
to change directory. I'm going to drag and drop this in here and I will Delete
the names. I'm just going to go to the folder it's in, so in this case the Desktop folder.
So now I'm there and I can do Sort and the name of the file. It's going to output all of that to
the Terminal window. I don't want it there. I want it in a file. So I'm going to modify
that and type the name of the file and then do dashO and give it a name for the new file. Now
I'll get a new file here. If I open that one up I'll see that sorted out alphabetically.
Now as it turns out there's a ton of functionality in the Sort command. You could do MAN for manual
and Sort and then you could look at the description for everything it does. So,
for instance, if I wanted to Sort in reverse order I could do Sort, names.txt, and then dashR
and it will do it in reverse order or dash capital R and it sorts them in random order.
I could even do this and do K2 for the second column and then define the column with dash T
and then in single quotes a space. So that's going to define this as 2 columns with a space
between each column and it's going to certify the second column, in other words in this case
a last name sort. You could see now sorted by last name. Of course it gets a little more complicated
if there is some middle names or some last names that are two words and things like that.
Now another way to do this with Terminal is to use one of the many Text Editors
inside of Terminal. One that will handle sorting is vim. So we're going to run vim and feed it in
this text file. So we're going to be editing the text file in this text editor. So now you're
actually in an app here. I can use the arrow keys to move the cursor around and edit the text. But
all I'm actually going to do is do the colon which takes me to a command line at the bottom
and I'm going to run the command Sort. That sorts all the lines in this file. Now I could do colon
again and then do W for write and Q for Quit. Now it quits and saves that back out. I won't see the
change here in TextEdit but if I close it and open it up again now you could see it's sorted by name.
Now if you Goggle vim and Sorting Options you'll see that there's a really deep set
of commands there and you could do all sorts of things in vim if you're handy with code.
Now, of course you can also get a text editor that's capable of sorting. There are lots that
you can pay for but one that's free that I highly recommend is CotEditor. You can get that in the
Mac App Store. You open this up with CotEditor you can actually sort right here by going to Text,
Line, and then Sort. You could also use sort by pattern. Sort by pattern is pretty strong
but it doesn't require much coding. So, for instance, I could say I want to do it by column,
I want the Delimiter to be space and the Position to be the second column.
Then I can set it to ignore case and other things. So now it will sort by last name.
Finally if you want you can create a shortcut in the new Shortcuts App
in macOS Monterey to do this. So you want to set it in the Shortcut Settings
to use as a Quick Action in the Services Menu. Receive text input from Quick Actions and if
it doesn't get anything Ask for a File. Then get the text from the Shortcut Input,
split the text by new lines, so every line becomes a different element in a list.
Then you're going to filter that. Now filters are a tricky one because it actually
shows up as Filter Files. This one here. But you're not filtering files here. That's okay.
It will work for other things. So use Filter Files and then have it take the input of the split text
and then don't do any filter at all. Just set it to Sort By Name. Then you use Combine to combine
the files back in the new lines and Save the combined text. So now when I run this it's going
to prompt me for a file, I'll select Names, and then it's going to prompt me where to save it.
I'll select the Desktop. So it's going to name it automatically after the beginning of the file. So
the first name in there. But you could easily go and change that. If I open it up you can see
now I've got a sorted list and I have a handy little shortcut that will do it for me anytime.
So there are six different ways to quickly
sort a list on your Mac. Hope you found this useful. Thanks for watching.