Fixed sort by state function.