sorting function, second draft