“sort by” only applies to the “return” set, so $c and $i are out of scope for the sort by. In the example, you need to sort by the text node of the city element rather than the city element. With Tamino, one way to sort by the text content is:
declare namespace xs = “XML Schema”
for $city in distinct-values(input()/Document/City)
let $i := input()/Document/City[. = $city]
return {count($i)}
sort by (xs:integer(text()) descending)
(I assume you want counts sorted numerically rather than alphanumerically, so the xs:integer fuction should be used)
Depending on the flexibility of what you can use for a return document, it may be clearer to return name and count as separate elements or attributes:
declare namespace xs = “XML Schema”
for $city in distinct-values(input()/Document/City)
let $i := input()/Document/City[. = $city]
return {$city}{count($i)}
sort by (xs:integer(count) descending)
#API-Management#webMethods#Tamino