How to sort the result from string_agg()
For Microsoft SQL: Use "WITHIN GROUP"
https://docs.microsoft.com/en-us/sql/t-sql/functions/string-agg-transact-sql?view=sql-server-2017
SELECT
STRING_AGG(prod, '|') WITHIN GROUP (ORDER BY product)
FROM ...
select string_agg(prod,' | ') FROM
(SELECT product as prod FROM tblproducts ORDER BY product )MAIN;
SQL FIDDLE
With postgres 9.0+ you can write:
select string_agg(product,' | ' order by product) from "tblproducts"
Details here.