Refactored sorting to use cmp.  Added tests.