sort and randomize functions