title: Sparql group_concat - Stop Ordering
body: <p>I'm fairly new to SPARQL, and am experiencing some interesting behaviour that I do not understand.</p>
  
  <p>So I have four genes: </p>
  
  <pre><code>a-gene
  b-gene
  c-gene
  d-gene
  </code></pre>
  
  <p>and two strains: </p>
  
  <pre><code>strain1
  strain2
  </code></pre>
  
  <p>and the following triples: </p>
  
  <pre><code>strain1 hasGene a-gene
  strain1 hasGene b-gene
  strain1 hasGene d-gene
  
  strain2 hasGene b-gene
  strain2 hasGene d-gene
  </code></pre>
  
  <p>My goal is to make a SPARQL query that adds a value to the property <code>hasBinary</code> for all strains, where <code>hasBinary</code> is the corresponding binary for what genes a strain has and has not. 
  eg: </p>
  
  <pre><code>strain1 hasBinary 1101
  strain2 hasBinary 0101
  </code></pre>
  
  <p><code>strain1</code> has gene <code>a-gene</code>, b-gene, <code>d-gene</code>, but not <code>c-gene</code>.
  Given the query (note strains and genes are in classes Strain and Gene, respectively):</p>
  
  <pre><code>select ?s (group_concat(?result ; separator="") as ?binary)
  where { ?g a Gene.
          ?s a Strain.
          optional{?s ?hasGene ?g.}.
          bind((if(bound(?hasGene), "1","0")) as ?result). }
  group by ?s
  order by ?s
  </code></pre>
  
  <p>The output is:</p>
  
  <pre><code>strain1 1101
  strain2 0101 
  </code></pre>
  
  <p>Which is correct. But when I do the query:</p>
  
  <pre><code>construct {?s hasBinary ?binary}
  where{
  
  select ?s (group_concat(?result ; separator="") as ?binary)
  where { ?g a Gene.
          ?s a Strain.
          optional{?s ?hasGene ?g.}.
          bind((if(bound(?hasGene), "1","0")) as ?result).
  
  
        }
  group by ?s
  order by ?s
  
  }
  </code></pre>
  
  <p>The output is:</p>
  
  <pre><code>strain1 hasBinary 0111
  strain2 hasBinary 0011
  </code></pre>
  
  <p>Which is totally wrong. It's as though <code>group_concat</code> is ordering the result. I have no idea why it is doing this, the binary is useless if it's ordered. Any help with this issue would be appreciated.</p>