added test and fixed sorting bug