modification for sorting based threshold encoding