Close tree before sort