It seems the difference between both codes is the manual shuffling of the indices?
How did you define your Dataset? Could it be that the classes are sorted internally?
If so, the second approach would pass only the first classes to the train_sampler and the last classes to the valid_sampler, which could explain your observation.