fix sort and encoding