"quantile" function in Stan?

Yes, if they’re just data, you can do that.

Otherwise, you need to sort and select (and maybe interpolate if you really want to go to town), as @sakrejda was outlining.

We probably should have a quantile function for data and generated quantities. It doesn’t make much sense in the model block as the sorting itself is going to act kind of like a step function (at some threshold, a different variable contributes, but that threshold’s not itself contributing to derivatives, but set by another element).