title: Why when I do sort by multiple variables, it only sorts by the last one listed?
body: <p>This is the raw SQL query I am sending</p>
  
  <pre><code>SELECT name, tags, components, metas, specials,
    array_length(ARRAY(SELECT * FROM UNNEST(components) WHERE UNNEST = ANY(ARRAY['a','b'])), 1) AS componentmatch,
    array_length(ARRAY(SELECT * FROM UNNEST(specials) WHERE UNNEST = ANY(ARRAY['m'])), 1) AS specialmatch,
    array_length(ARRAY(SELECT * FROM UNNEST(metas) WHERE UNNEST = ANY(ARRAY['v', 't'])), 1) AS metamatch 
  FROM films
  WHERE tags &amp;&amp; ARRAY['a','b', 'm', 'v', 't']
  ORDER BY componentmatch DESC, metamatch DESC, specialmatch DESC
  LIMIT 5
  </code></pre>
  
  <p>If I understand correctly how it works, it should sort by metamatch, then, if metamatch is the same, use componentmatch and only then specialmatch.</p>
  
  <p>However, it only sorts by specialmatch and ignores other criteria completely. I tried shuffling the order and found out that it always sorts by last criteria listed.
  Am I doing something wrong?</p>
  
  <p>P.S. I suspect that it might be due to the fact that I sort it by projected variables.</p>
  
  <p>Query:</p>
  
  <pre><code>:components =&gt; ["c", "ca"], :specials =&gt; ["4/7"], :metas =&gt; []
  </code></pre>
  
  <p>Sample output:</p>
  
  <pre><code>{:name=&gt;"Rydge", :component_tags=&gt;[], :special_tags=&gt;["4/7"], :metas=&gt;["Lodge"], :tags=&gt;["4/7", "Lodge"], :componentmatch=&gt;nil, :categorymatch=&gt;nil, :specialmatch=&gt;1}
  {:name=&gt;"Ranada", :component_tags=&gt;[], :special_tags=&gt;["4/7"], :metas=&gt;["Place"], :tags=&gt;["4/7", "Place"], :componentmatch=&gt;nil, :categorymatch=&gt;nil, :specialmatch=&gt;1}
      {:name=&gt;"Nomads", :components=&gt;["a"], :specials=&gt;["4/7"], :metas=&gt;["Place"], :tags=&gt;["a", "4/7", "Place"], :componentmatch=&gt;nil, :categorymatch=&gt;nil, :specialmatch=&gt;1}
  {:name=&gt;"Vido", :components=&gt;["a"], :specials=&gt;["4/7"], :metas=&gt;[], :tags=&gt;["a", "4/7"], :componentmatch=&gt;nil, :categorymatch=&gt;nil, :specialmatch=&gt;1}
  {:name=&gt;"New Nomen", :components=&gt;["a", "fa"], :specials=&gt;["4/7"], :metas=&gt;["Place"], :tags=&gt;["a", "fa", "4/7", "Place"], :componentmatch=&gt;nil, :categorymatch=&gt;nil, :specialmatch=&gt;1}
  </code></pre>
  
  <p>As you can see, it returns those rows that match specials, but components are ignored although they come before specials in sort.
  P.S. I can assure that components values I am querying the table for exist in it, so they are not ignored because they don't exist.</p>