I have a class Users:
class User
{
public string Name { get; set; }
public Address Address { get; set; }
public DateTime Birthday { get; set; }
}
Then I have a list of users like List<User> users
. And I meet with the problem which sounds "How to order list by Name length string property?
I've tried something like this:
users.OrderBy(user=>user.Name.Length);
and unfortunately it didn't work.
Thanks for the reply and best regards.
Providing that the list doesn't have null
users as well as null
names:
users.Sort((Comparison<User>) ((left, right) =>
return left.Name.Length - right.Name.Length;
));
Note, that the code sorts an existing list users
, while OrderBy
creates a new collection :
users = users
.OrderBy(user => user.Name.Length)
.ToList();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.