Shayan/sort encode