Heir mechanics are fairly random.
Usually goes to your current king's oldest son (or if the king died, the heir's oldest son), but not even that stands true all the time.