Sort output of plpgsql function
While returning rows from a loop directly, the sort order is determined by the loop. PL/pgSQL builds up the result set and you can still raise an exception to prevent the function from returning anything until the last moment. (There is a code example in the manual.) But there is no way to re-order the result set before finally returning. You would have to add ORDER BY to the function call for that:

SELECT * FROM public.compare_schemas_by_table_emptiness('foo', 'bar')
ORDER  BY match;


FALSE sorts before TRUE, so non-matches come first.


  But I'm really only interested in the cases where the output's match column is false.


So don't do more work than required. This function does what you need, efficient and safe against SQL injection (unlike your original):

CREATE OR REPLACE FUNCTION public.compare_schemas_by_table_emptiness(_schema1 text, _schema2 text) 
  RETURNS TABLE(tablename text, schema1_ct bigint, schema2_ct bigint)
  LANGUAGE plpgsql AS
$func$
BEGIN
   FOR tablename IN
      SELECT quote_ident(t.tablename)  -- must be table-qualified, due to dupe in OUT parameters!
      FROM   pg_catalog.pg_tables t
      WHERE  t.schemaname = _schema1
      ORDER  BY 1
   LOOP
      EXECUTE format(
         'SELECT (SELECT count(*) FROM %1$I.%3$s)
               , (SELECT count(*) FROM %2$I.%3$s)
          WHERE  EXISTS (SELECT FROM %1$I.%3$s)
              &lt;&gt; EXISTS (SELECT FROM %2$I.%3$s)'
       , _schema1, _schema2, tablename)
      INTO schema1_ct, schema2_ct;

      CONTINUE WHEN schema1_ct IS NULL; -- happens when both or none have values
      RETURN NEXT;
   END LOOP;
END
$func$;


Call:

SELECT * FROM public.compare_schemas_by_table_emptiness('foo', 'bar');


Counting can be expensive with big tables, so this function only counts when required.

Identifiers may or may not require double-quoting and have to be treated like user input to be safe. See:


SQL injection in Postgres functions vs prepared queries