correct sorting function