I have an array of objects, each object containing a user id, and an amount of money. How would I sort this array to make the objects with the largest sum of money be the first elements in the array, and the last ones being the people with the least amount of money?
Asked
Active
Viewed 355 times
-1
-
please share your code – imsome1 Mar 04 '17 at 03:51
-
http://stackoverflow.com/questions/3309188/how-to-sort-a-listt-by-a-property-in-the-object?rq=1 – M.kazem Akhgary Mar 04 '17 at 03:55
2 Answers
2
I would do it with LINQ 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);
}
}

Andrew Kovalenko
- 6,441
- 2
- 29
- 43
0
Order by ascending :
var newList = YourObj.OrderBy(x => x.Price).ToList();
Order by Descending:
var newList = YourObj.OrderByDescending(x => x.Price).ToList();

Ray
- 188
- 2
- 17
-
If you want to answer question with plenty of duplicates at least provide an answer. `List
` is not array. – Alexei Levenkov Mar 04 '17 at 04:05