I think that sorting is a bit brutal but it makes the entire logic of optimizeClauses vivid and simple.  Of course we can use unordered_set or something alike, it makes code faster, but not simpler.   May be I just don't get the idea. 