sindresorhus / Actions

βš™οΈ Supercharge your shortcuts
https://sindresorhus.com/actions
2.65k stars 115 forks source link

Action proposal: Sort dictionary #118

Open madysondesigns opened 1 year ago

madysondesigns commented 1 year ago

Description

The Sort List action you already have is super useful, but it falls short if you're dealing with more complex data. It doesn't work on dictionaries at all, I imagine because it can't parse the additional nesting.

Some features I think would be useful:

(One other small piece of dictionary-related feedback: IMO, the Pretty Print Dictionaries action belongs in the Dictionary category rather than Formatting. As far as I can tell it's only for display and doesn't actually transform what's returned, so I don't think of it as formatting and I can never find it in the list. It also applies to a specific data type that you've got a category for as opposed to the other formatting actions which work on specific types of content and aren't tied to a data type. Alternatively, it might make sense under a new category for Output along with the existing Spell Out Number and any modal or other pretty display actions mentioned in that other issue πŸ™‚)

In general, thank you thank you thank you for all the work you put into creating such a useful app! You should charge money for it – this is something I would happily pay $5 or $10 to unlock full functionality for!

sindresorhus commented 1 year ago

The Sort List action you already have is super useful, but it falls short if you're dealing with more complex data. It doesn't work on dictionaries at all, I imagine because it can't parse the additional nesting.

Are you saying it doesn't work for a list of dictionaries or just a single dictionary?

Ability to sort by either key or value

Are you requesting this for a single dictionary or a list of dictionaries? Because a dictionary does not preserve its sort order, so sorting the keys of a single dictionary would not be possible.

sindresorhus commented 1 year ago

(One other small piece of dictionary-related feedback: IMO, the Pretty Print Dictionaries action belongs in the Dictionary category rather than Formatting. As far as I can tell it's only for display and doesn't actually transform what's returned, so I don't think of it as formatting and I can never find it in the list.

Everything in the formatting category is meant for display-only. Formatting is for things you want to present to humans.

It's unfortunately that an action cannot be in multiple categories. I do think it's more correct to have it in formatting though. The dictionary category is for transforming dictionaries and this is not a transformation, since the pretty print formatting is lost if you use any dictionary actions on it afterwards.

madysondesigns commented 1 year ago

The Sort List action you already have is super useful, but it falls short if you're dealing with more complex data. It doesn't work on dictionaries at all, I imagine because it can't parse the additional nesting.

Are you saying it doesn't work for a list of dictionaries or just a single dictionary?

Ability to sort by either key or value

Are you requesting this for a single dictionary or a list of dictionaries? Because a dictionary does not preserve its sort order, so sorting the keys of a single dictionary would not be possible.

Ah, TIL. The dictionaries I've created have seemed to return their keys or values consistently in the order I added them but that must have been coincidental.

The dictionary I'm working with now contains data from an API and using 'Choose from list' shows the keys in a random order every time which proves your point though. I solved that problem by using the 'Filter files' action on the keys first, which has a sorting option.

I do think sorting a list of dictionaries would be useful in scenarios like 'Parse CSV', since CSVs may be created with sorted data. Perhaps your 'Sort list' action could be modified to input and output a list of dictionaries in addition to a text list.

madysondesigns commented 1 year ago

(One other small piece of dictionary-related feedback: IMO, the Pretty Print Dictionaries action belongs in the Dictionary category rather than Formatting. As far as I can tell it's only for display and doesn't actually transform what's returned, so I don't think of it as formatting and I can never find it in the list.

Everything in the formatting category is meant for display-only. Formatting is for things you want to present to humans.

It's unfortunately that an action cannot be in multiple categories. I do think it's more correct to have it in formatting though. The dictionary category is for transforming dictionaries and this is not a transformation, since the pretty print formatting is lost if you use any dictionary actions on it afterwards.

I buy that 'Dictionary' may not be the right place for it, but I still think separate categories for non-transforming display actions and true formatting ones is a helpful distinction from a user's perspective.

I think the 'Formatting' category trips me up because some of the actions do transform the data and some don't – e.g. passing an integer to 'Format Currency' outputs text with the currency symbol and decimal places:

IMG_9526

sindresorhus commented 1 year ago

I do think sorting a list of dictionaries would be useful in scenarios like 'Parse CSV', since CSVs may be created with sorted data. Perhaps your 'Sort list' action could be modified to input and output a list of dictionaries in addition to a text list.

I agree. It could be useful to sort a list of dictionaries based on a key path to a dictionary key. I will think about whether it makes sense as part of the existing action or if it should be a new one.

I think the 'Formatting' category trips me up because some of the actions do transform the data and some don't – e.g. passing an integer to 'Format Currency' outputs text with the currency symbol and decimal places:

I think we have a different view on what "formatting" means. The Format Currency action uses a formatting API to format the number as a currency:

amount.formatted(.currency(code: currencyCode))

passing an integer to 'Format Currency' outputs text with the currency symbol and decimal places:

Currencies don't differentiate between integer and floating point, neither does Shortcuts.

madysondesigns commented 1 year ago

I think we have a different view on what "formatting" means. The Format Currency action uses a formatting API to format the number as a currency:

amount.formatted(.currency(code: currencyCode))

passing an integer to 'Format Currency' outputs text with the currency symbol and decimal places:

Currencies don't differentiate between integer and floating point, neither does Shortcuts.

I would consider a formatting action to be anything where output =/= input, in type and/or content. I mentioned integer only as a type of number, the action takes a number type and outputs text type, in addition to changing the content. It doesn't have anything to do with number vs. integer really, that was a red herring on my part.

madysondesigns commented 1 year ago

I agree. It could be useful to sort a list of dictionaries based on a key path to a dictionary key. I will think about whether it makes sense as part of the existing action or if it should be a new one.

Either way it would be an excellent addition! Appreciate the detailed context and that you're so receptive to feedback πŸ˜„