Large Language Models (LLMs) don't "understand" code the way you do. They predict the most likely next token based on patterns in their training data.
When you write "function to sort an array," the model has seen millions of sorting functions. It generates code that statistically resembles those examples. This works well for common patterns but has limits you'll learn to recognize.