                 collect.append(r)
             }
         }
-        return RangeSet(bounds: collect)
+        return RangeSet(ranges: collect.sorted { $0.lowerBound < $1.lowerBound })
     }
     
     public func intersection(_ range: Range<Bound>) -> RangeSet {
         var collect: [Range<Bound>] = []
-        for r in self where range.overlaps(r) {
+        for r in ranges where range.overlaps(r) {
             collect.append(r.clamped(to: range))
         }
-        return RangeSet(bounds: collect)
+        return RangeSet(ranges: collect.sorted { $0.lowerBound < $1.lowerBound })
     }
 }


public struct RangeSet<Bound : Comparable> {
    
    public let lowerBounds: [Bound]
    public let upperBounds: [Bound]
    
    public init() {
        self.lowerBounds = []
        self.upperBounds = []
    }
    public init(_ ranges: Range<Bound> ... ) {
        self.init(ranges)

    public func union(_ range: Range<Bound>) -> RangeSet {
        var collect: [Range<Bound>] = []
        var overlap = range
        for r in ranges {
            if overlap.overlaps(r) || overlap.lowerBound == r.upperBound || overlap.upperBound == r.lowerBound {
                overlap = Range(uncheckedBounds: (Swift.min(overlap.lowerBound, r.lowerBound), Swift.max(overlap.upperBound, r.upperBound)))
            } else {
