How to sort an array of objects by a string property in JavaScript?
Hello everyone! I'm new to JavaScript and can't figure out how to sort an array of objects.
I have an array with objects that contain user information: name and age. Here's what it looks like:
```javascript
const users = [
{ name: 'Иван', age: 25 },
{ name: 'Анна', age: 30 },
{ name: 'Петр', age: 22 },
{ name: 'Елена', age: 28 }
];
```
I need to sort this array by name (the name field) in alphabetical order. I know that for regular arrays you can use the `sort()` method, but it doesn't work directly for objects. I tried writing something like:
```javascript
users.sort();
```
But it didn't work — the array remained in its original order. Then I tried to pass a comparison function, but got confused with the logic.
Please tell me how to correctly