Added option to sort by state

Closes #1772