按C#中的值对对象数组进行排序?
我有一个对象数组,每个对象包含一个用户id和一笔钱。如何对该数组进行排序,使金额最大的对象成为数组中的第一个元素,最后一个元素是金额最少的人?我将使用LINQ按C#中的值对对象数组进行排序?,c#,C#,我有一个对象数组,每个对象包含一个用户id和一笔钱。如何对该数组进行排序,使金额最大的对象成为数组中的第一个元素,最后一个元素是金额最少的人?我将使用LINQorderBy class Pet { public string Name { get; set; } public int Age { get; set; } } public static void OrderByEx1() { Pet[] pets = { new Pet { Name="Barley",
orderBy
class Pet
{
public string Name { get; set; }
public int Age { get; set; }
}
public static void OrderByEx1()
{
Pet[] pets = { new Pet { Name="Barley", Age=8 },
new Pet { Name="Boots", Age=4 },
new Pet { Name="Whiskers", Age=1 } };
IEnumerable<Pet> query = pets.OrderBy(pet => pet.Age);
foreach (Pet pet in query)
{
Console.WriteLine("{0} - {1}", pet.Name, pet.Age);
}
}
class宠物
{
公共字符串名称{get;set;}
公共整数{get;set;}
}
公共静态无效OrderByEx1()
{
宠物[]宠物={新宠物{Name=“大麦”,年龄=8},
新宠物{Name=“Boots”,年龄=4},
新宠物{Name=“胡须”,年龄=1};
IEnumerable query=pets.OrderBy(pet=>pet.Age);
foreach(查询中的宠物)
{
Console.WriteLine(“{0}-{1}”,pet.Name,pet.Age);
}
}
按升序排列:
var newList = YourObj.OrderBy(x => x.Price).ToList();
按降序排列:
var newList = YourObj.OrderByDescending(x => x.Price).ToList();
请分享您的代码,如果您想回答大量重复的问题,请至少提供一个答案<代码>列表不是数组。