title: Handling parameterised order by filters within neo4j cypher
body: <p>I am working on the cypher below but I am not sure how to implement parameterised sorting. I want to sort with the parameters $field and $sort where $field could be: 'species.name', 'species.description', 'species.scientificName', 'monthCount', 'eats'  or 'eatenBy' and $sort is only 'asc' or 'desc'. If either of those values is hardcoded then the cypher runs but when passed as a parameter it fails. Any help would be greatly appreciated! :)</p>
  <pre><code>MATCH (s:Species)
    WHERE toLower(s.name) CONTAINS toLower($search)
  
  WITH s
  
  OPTIONAL MATCH (s)-[e:EATS]-&gt;(eatsSpecies:Species)
  OPTIONAL MATCH (s)&lt;-[:EATEN_BY]-(eatenBySpecies:Species)
  OPTIONAL MATCH (s)&lt;-[:IS_ABOUT]-(image:Image)
  OPTIONAL MATCH (s)-[:FALLS_UNDER]-&gt;(primary:Primary)
  OPTIONAL MATCH (s)-[:MEASURED_BY]-&gt;(month:Month)
  
  WITH s, eatsSpecies, eatenBySpecies, image, primary, month
  
  WITH s,
       count(DISTINCT eatsSpecies.name) AS eats,
       count(DISTINCT eatenBySpecies.name) AS eatenBy,
       primary,
       image,
       count(distinct month) as monthCount
  
  WITH {
         name:              s.name,
         scientificName:    s.scientificName,
         description:       s.description,
         primary:           case when exists(primary.GUID) then true else false end,
         active:            case when exists(s.active) then s.active else true end,
         months:            monthCount,
         guid:              s.GUID,
         eats:              eats,
         eatenBy:           eatenBy,
         image: case when exists(image.url) then true else false end
       } AS species order by $field $sort
  
  SKIP $skip
  LIMIT $limit
  
  RETURN collect(species)
  
  </code></pre>