Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Built-in function to sort an array #2251

Closed
stan-sz opened this issue Apr 14, 2021 · 4 comments · Fixed by #7083
Closed

Built-in function to sort an array #2251

stan-sz opened this issue Apr 14, 2021 · 4 comments · Fixed by #7083

Comments

@stan-sz
Copy link
Contributor

stan-sz commented Apr 14, 2021

Is your feature request related to a problem? Please describe.
Declarative deployments require consistent inputs for idempotency. I have a bicep template where a part of a blob name is computed using uniqueString(array). I'd like to ensure within the template that the array is always sorted to yield the same results for the same inputs.

Describe the solution you'd like
Given that ARM functions already have a defined compare functions, add a sort( function that would sort an array. Somewhat related to #2082

@stan-sz stan-sz added the enhancement New feature or request label Apr 14, 2021
@ghost ghost added the Needs: Triage 🔍 label Apr 14, 2021
@majastrz
Copy link
Member

We could be sorting arrays of objects with some properties or arrays of primitive types like strings or ints. How do you envision selecting the "key" for sorting the elements of the array?

@stan-sz
Copy link
Contributor Author

stan-sz commented Apr 14, 2021

I think the scope would be to write a meta sorting algorithm using the existing greater( or less( functions. It would be up to these functions to handle (or not) objects - the documentation and examples are missing this part. The selection whether to do ascending/descending order sorting could be a bool parameter of the sort function.
This also reminds me of #1427 to be able to iterate over dictionary keys in general.

@bmoore-msft
Copy link
Contributor

@stan-sz - can you expand on the scenario a bit? JSON arrays are not sorted so the platform won't treat this consistently...

Also, as I understand your scenario, I'd just use a different / deterministic seed for the array.

@stan-sz
Copy link
Contributor Author

stan-sz commented Apr 28, 2021

Here's the detailed scenario: the Microsoft.ContainerRegistry/registries/pipelineRuns is used to transfer OCI artifacts (e.g. docker images, helm charts) from one container registry to another. One of the inputs is an array of to-be-transferred artifacts the other param is the name of the blob where the images will be exported to. uniqueString(array) is very handy to compute a unique blob name for the set of artifacts to be transferred.

The 'receiving' side of this pipeline needs to set the same blob name to trigger the import pipeline to get the images from the blob into the target container registry. Again, uniqueString(array) is handy in this case, as it returns the same unique string.

Currently a pre-deployment script is used to prepare the ARM parameters file based on user input (if the user wants to transfer a subset of images) to ensure the array is sorted. The user selects the images based on apps (each app consists of one or more images) and thus the array is the union of all images from selected apps.
With the sorting ability in Bicep, the whole logic of selecting a subset of images could be moved into the template and the user would interact only with the ARM deployment by passing the list of apps to transfer (the template would get as an input also the app->images mapping to do the image selection).

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants