Sort branches intelligently