Skip to content

Array functions and order preservation #15924

@zeen

Description

@zeen

Arrays are an ordered data structure. For array functions that generate new arrays based on existing ones, the documentation isn't clear on whether there's any order that can be assumed for the result.

Functions of interest:

  • array_intersect
  • array_except
  • array_union
  • array_distinct

Based on my testing, order seems to be preserved. For the one or more input arrays being operated on, output ordering is based on the input arrays' order, with the first array's order having priority over the second's, etc.

e.g., for array_intersect(array['a', 'b', 'c'], array['d', 'c', 'b']) the result is consistently ['b', 'c'], and not ['c', 'b']. Can this behavior be relied upon?

The following also lack any explicit statement in the docs, but it would be highly surprising if we couldn't assume the order of the returned array is based on the input array(s):

  • array_remove
  • concat
  • combinations
  • filter

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions