sort and merge