fixing sort logic