title: Sort data before concatenating using STUFF FOR XML
body: <p>I have the following query that I am using for an SSRS Report:</p>
  
  <pre><code>SELECT  ROW_NUMBER() OVER ( ORDER BY Judge.EventJudgeID ) AS JudgeRow ,
          Judge.EventID ,
          Judge.Judge_PersonID ,
          STUFF(( SELECT DISTINCT
                          ',' + CAST(Fights.FightNumber AS VARCHAR(MAX)) AS [text()]
                  FROM    dbo.tblFights Fights ,
                          dbo.tblFightJudge FRJudge
                  WHERE   Fights.FightID = FRJudge.fightid
                          AND ( Judge.Judge_PersonID = FRJudge.judge1id
                                OR Judge.Judge_PersonID = FRJudge.judge2id
                                OR Judge.Judge_PersonID = FRJudge.judge3id
                              )
                FOR
                  XML PATH('')
                ), 1, 1, '') AS BoutsJudged ,
          Persons.LastName + ' ' + Persons.FirstName AS JudgeName ,
          Events.EventName ,
          Events.EventDate
  FROM    dbo.tblEventJudge Judge
          INNER JOIN dbo.tblPersons Persons ON PersonID = Judge_PersonID
          INNER JOIN dbo.tblEvents Events ON Events.EventID = Judge.EventID
  WHERE   Judge.EventID = 1278;
  </code></pre>
  
  <p>The problem is that the STUFF command returns the following string:</p>
  
  <p>1,10,11,12,13,14,15,16,17,18,19,2,3,4,5,6,7,8,9</p>
  
  <p>How can I make it sort the numbers before concatenating it into a string?</p>