Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8674209
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:39:11+00:00 2026-06-12T19:39:11+00:00

I have a table with 4 array columns.. the results are like: ids signed_ids

  • 0

I have a table with 4 array columns.. the results are like:

ids       signed_ids   new_ids   new_ids_signed
{1,2,3} | {2,1,3}    | {4,5,6} | {6,5,4}

Anyway to compare ids and signed_ids so that they come out equal, by ignoring the order of the elements?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T19:39:12+00:00Added an answer on June 12, 2026 at 7:39 pm

    The simplest thing to do is sort them and compare them sorted. See sorting arrays in PostgreSQL.

    Given sample data:

    CREATE TABLE aa(ids integer[], signed_ids integer[]);
    INSERT INTO aa(ids, signed_ids) VALUES (ARRAY[1,2,3], ARRAY[2,1,3]);
    

    the best thing to do is to if the array entries are always integers is to use the intarray extension, as Erwin explains in his answer. It’s a lot faster than any pure-SQL formulation.

    Otherwise, for a general version that works for any data type, define an array_sort(anyarray):

    CREATE OR REPLACE FUNCTION array_sort(anyarray) RETURNS anyarray AS $$
    SELECT array_agg(x order by x) FROM unnest($1) x;
    $$ LANGUAGE 'SQL';
    

    and use it sort and compare the sorted arrays:

    SELECT array_sort(ids) = array_sort(signed_ids) FROM aa;
    

    There’s an important caveat:

    SELECT array_sort( ARRAY[1,2,2,4,4] ) = array_sort( ARRAY[1,2,4] );
    

    will be false. This may or may not be what you want, depending on your intentions.


    Alternately, define a function array_compare_as_set:

    CREATE OR REPLACE FUNCTION array_compare_as_set(anyarray,anyarray) RETURNS boolean AS $$
    SELECT CASE
      WHEN array_dims($1) <> array_dims($2) THEN
        'f'
      WHEN array_length($1,1) <> array_length($2,1) THEN
        'f'
      ELSE
        NOT EXISTS (
            SELECT 1
            FROM unnest($1) a 
            FULL JOIN unnest($2) b ON (a=b) 
            WHERE a IS NULL or b IS NULL
        )
      END
    $$ LANGUAGE 'SQL' IMMUTABLE;
    

    and then:

    SELECT array_compare_as_set(ids, signed_ids) FROM aa;
    

    This is subtly different from comparing two array_sorted values. array_compare_as_set will eliminate duplicates, making array_compare_as_set(ARRAY[1,2,3,3],ARRAY[1,2,3]) true, whereas array_sort(ARRAY[1,2,3,3]) = array_sort(ARRAY[1,2,3]) will be false.

    Both of these approaches will have pretty bad performance. Consider ensuring that you always store your arrays sorted in the first place.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have written a function to print database table to an array like this
Suppose I have an array that mimics a database table. Each array element represents
Here's my problem: I have a table with 4 columns (type = array, one-dimension)
I have a table array which looks like this: tablearray = [ {'column1': 1,
I have some code that creates a table like this: for($i=0; $i<$fields_num; $i++) {
i have a three dimensional bit table array as bit_table[dim1][100][200]; The second and third
I have fetched the contents of a table in array of object. And the
While working with ActiveRecord I have a table which stores a serialized array of
I've got an array of names: $names = array('ray'=>0,'bob'=>1,'sue'=>2,'jeff'=>3); Then I have a table
Say I have an array of table DDL queries and I want to get

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.