sort function made and verified mostly