add sorting functionality and some test cases