modified sort_by_pval and unit tests