This is due to the way Scala Templates works. I suspect that your TreeSet collection is under the hood mapped to another collection and as a result the ordering is not preserved.
Obviously, there is a difference between the behavior of the Scala for loop and the for loop in Scala patterns. If you run your code as normal Scala code, the order of the TreeSet is obviously preserved:
val users = TreeSet("foo", "bar", "zzz", "abc") for (user <- users) { println(user) }
One way to solve the problem is to use an iterator in a Scala template:
@for(name <- usernames.iterator) { @name , }
or convert a TreeSet to a sequence:
@for(name <- usernames.toSeq) { @name , }
source share