The insight: sort both arrays. Then match the smallest cookie to the smallest satisfiable child. Why? If a small cookie can satisfy a child, using a larger cookie wastes capacity. Larger cookies should be saved for greedier children.
More formally: if cookie can satisfy child , any larger cookie can too. Using the smallest sufficient cookie leaves more options for harder children. The sort order is the core idea. If you sorted descending instead of ascending, greedy would fail. Choosing the right sort is what makes greedy work.