Get dictionary based on smallest value

I have a dictionary which contains bus stop data in the following format:

    "name": "10th St & Cumberland St",
    "id": 16102,
    "distance": 3.814320304879956
}, {
    "name": "10th St & Dauphin St",
    "id": 16104,
    "distance": 3.62157076638738
}, {
    "name": "10th St & Susquehanna Av",
    "id": 16105,
    "distance": 3.518528798201017
}, {
    "name": "10th St & York St",
    "id": 16103,
    "distance": 3.716352780256479

It’s about 200 objects long. I want to select the 5 objects where “distance” contains the smallest values.

I hope this shortcut will give you an approach to consider utilising.

As it stands I’ve tweaked one of the distances to give it an extra integer digit to ensure the sorting accounts for “” being smaller than “”, and given there are just five values, the filtering restriction I’ve set as two otherwise it would always return the entire set from this sample data.

You also haven’t said what it is of the ‘objects’ you want at the end, so for illustration purposes I’ve retrieved all three items of data (the id, name and distance), plus the sequence order (1st smallest, distance, 2nd smallest distance) and output that as some rich text. you would change that end processing to use and work with the data in whatever way you deem necessary.

Hopefully the algorithm makes sense and this helps, but if there are questions I’m happy to answer them.

1 Like

Algorithm looks good.

One suggestion to optimise the Shortcut itself…

If I’m reading it correctly, the actions at the start of the first Repeat loop are all about padding the integer part of the distance and then trimming it to a fixed length.

I think this could be simplified by using a Replace Text action with a Regular Expression as illustrated below.

1 Like

They certainly could. :+1:t2:

I actually didn’t want to make it more opaque to the OP given the basis of the request and what looked like a slightly unusual fixed format to the data, so I specifically avoided either regexing the distance text as you set out, or evaluating the distance directly with an evaluated JavaScript -based numeric sort of the lines without the padded numeric versions of the strings. I just figured sticking to incremental, simple text-based working was the most easily understood approach was all.

Just curious: Is there a reason not to treat the distances as numbers and find the smallest? I haven’t used Shortcuts dictionaries from JSON much — is it that you’d have to convert text values to integers, or am I missing something?

The sorting applied by using file filtering is text based. Sorting the following numbers alphabetically…


… gives a non-numeric ordering. Padding with zeros aligns the ordering to be equivalent to numeric sorting…


You often get this scenario with sequence numbering on file name prefixes which is why it is good practice to pad those with leading zeros to an agreed length in the same way.

So shortcuts assumes all dictionary fields are text? There’s no way to treat values as numbers?

Yes, that is true. Dictionary/JSON format data is plain text and has no standard way to infer a data type for data within it.

But that wasn’t the reason for the zero padding. That step was because the built-in sorting option utilised can only sort on an alphabetic basis and not on a numeric value basis; see above.

I see. Thanks! I missed that part of the explanation and assumed a list of numbers would be sorted numerically. Hopefully they’ll get to that…

Well, it was one of a set of sorting suggestions I made to the team back on the initial release of Workflow, so I wouldn’t necessarily expect it in the next update, but I’m sure it must be on a backlog list somewhere. :laughing:

1 Like

Worth noting that the Toolbox Pro app provides a “Sort List” Shortcuts action that can sort a list of numbers.

I’ve only tried it briefly, but it seems to work ok.


Already noted, I was on the beta testing :wink:

1 Like

Here is another version to build from… a bit more simple using the Toolbox Pro action. Not as robust as the prior example, but assuming consistent format of data as posted, this should work fine.