Created sorting function