Is there sorting methods for Pulumi Outputs?
# general
g
Is there sorting methods for Pulumi Outputs?
b
can you elaborate on “sorting methods” ? We’ve recently added some new helper methods like
Output.json_dumps
in python
m
If you mean like built-in array-sorting methods on types like
Output<T[]>
, then no, but you can always sort the unwrapped values and return another
Output<T[]>
with the sorted result — for example in TypeScript:
Copy code
import * as aws from "@pulumi/aws";

// unsortedKeys is an Output<string[]>.
export const unsortedKeys = aws.s3
    .getObjectsOutput({ bucket: "some-bucket" })
    .keys;

// sortedKeys is also an Output<string[]>, but reverse-sorted.
export const sortedKeys = unsortedKeys
    .apply(keys => keys.reverse());
g
Yeah we are running into issues often when looping over a collection of values that when interpreted the values come back in various orders which causes occasional updates and unexpected values being given to calls we did not expect. While we worked through each of those cases with similar methods to the above, seems like something pulumi might account for, so hence I asked.
m
when interpreted the values come back in various orders which causes occasional updates and unexpected values being given to calls we did not expect
Ah, I see. If these values that vary are being returned by provider resources, then I think that’d be considered a bug. Are these AWS resources, for example?
g
They are, but I'm throwing them into arrays or maps most of the time. For example, I have a Map of Tags that I need to convert to a specific TagArrayInput for the new CloudControlApi abstraction. I have to first sort by the keys of the map, then convert, otherwise tags will end up in seemingly random order and of they do on each interpretation during Pulumi up, Pulumi deems that change in order an update. Had similar issue with a collection of subnets where it would have been very nice if I could have ordered that list of ids before looping over it. Ultimately resolved that similar to your approach.