I’ve included below shortcuts that manipulate lists. A number of these shortcuts rely on the Filter Files action, and the sort behavior of this action has apparently changed recently. These shortcuts were tested on macOS 15.6 only
A list of text or numbers can be sorted with the Filter Files action.
A list of dates can be sorted with the Filter Files action but the dates must first be converted to a sortable format (the ISO 8601 standard is used here). This shortcut requires further testing.
One can use Zsh shell features to sort ascending (o) and unique (u) in combination with a passed list to remove duplicates from that list. No n-tuple run shell scripts, no loops, and no regex.
VikingOSX Thanks for the suggestion, which works great. While testing, I added a Split Text action to your shortcut to return a list.
FWIW, the following takes a similar approach in that it sorts the list then removes duplicates. It’s not as compact as your suggestion, but it’s faster.
In an AppleScript, you can set the value of a particular item of an existing list to a new value, This doesn’t appear to be natively possible in a shortcut. This can be done with a repeat loop, although it can be slow if the list is large:
There are several approaches that can be used to add a value to the beginning or end of an existing list. The Add to Variable action is the fastest approach in my testing:
If you select the rows of the result and copy to the clipboard, you can then open the Terminal application and enter this command sequence to see that each line ends with a newline (0a):
As explained earlier, the only native action that can be used to sort a list in a shortcut is Filter Files. This action sorts in lexicographic (alphabetic) order, although numbers that are text are sorted numerically. The only option is a reverse sort. This action does a good job and takes about 5 milliseconds to sort a small list of 4 items on my M6 Mac mini.
When required, one alternative is to run the sort shell command in a Run Shell Script action. The benefits of this approach are its numerous sort options, including case-sensitive and numeric sorts. Two issues with this approach are:
The list has to be coerced to a string before being sorted and back to a list afterwards.
Because of the overhead of the Run Shell Script action, this approach takes about 80 milliseconds to sort a small list.
I retested the above shortcuts using a list that contained 10,000 items, and I timed the sort but not the list-creation actions. The timing result for the shortcut that used the Filter Files action was a respectable 70 milliseconds. I was unable to run a similar test with the shortcut that used the sort shell command, and the reason is that a generic error message was issued between the Combine Text and *Run Shell Script actions.
This screenshot documents the timing approach used with all shortcuts.
VikingOSX suggested using zsh shell parameter expansion to sort a list in a shortcut. This works well and has a number of useful sort flag options. The basic approach is as follows, and the timing result was about 80 milliseconds.
The sort flags that can be used with the approach are:
FLAG - OPTION
o - sort in ascending order
O - sort in descending order
i - case-insensitve order
n - numerical order
u - remove duplicates
This approach also failed when attempting to sort the large list. Just in case anyone wants to give it a try, this is VikingOSX’s shortcut that fails with the large list. The actual action that errors is Run Shell Script.
VikingOSX. If I understand your comment correctly, I agree that the Run Shell Script action outputs a string with 4 lines. The shortcuts in my posts immediately above return a Shortcuts list, and I wanted to do the same with your suggestion. This requires a Split Text action or something similar to create the list from text.
I just input a 435 line text file into the run shell script action. The Finder name is stopwords.txt, yet the File action decided to be redundant with the extension. The first non-comment item in the text file is the letter ‘a’.
It appears that without the Split action following that action, the Get Item action converts an array into a newline punctuated text. One cannot even add the Shortcut result to an append to list action either. PITA alert.
Although using a ruby two-liner to pass a confirmed Array class out of the Run Shell Script, I couldn’t get the first item until I incorporated the Split action as you did…
There are two actions that add values to a list, and their names are Add to Variable and Add Item to List. I’m not certain of this, but I think the Add Item to List action was added to the Shortcuts app in macOS Golden Gate.
The Add to Variable action “appends this action’s input to the specified variable, creating the variable if it does not exist.” The distinguishing characteristics of this action are that it creates the list if it doesn’t exist and added items are always appended to the end of the list.
The Add Item to List action “adds the specified item to a list at the beginning, end, or a specific index.” The distinguishing characteristics of this action are that the list must exist and the location of the added item within the specified list can be set. If the location is beginning or an index number, existing items in the list are moved forward to accommodate the new item.
The following example uses the Add to Variable action to create and add two items to a list and then uses the Add Item to List action to insert a value at the beginning of the list. I assume (but have no way of testing) that this shortcut will report an error if installed on macOS Tahoe or earlier.
BTW, the Add Item to List action can be used to merge two or more lists, and this includes the ability to set the position of the added list in the source list.