-1

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?

John Landon
  • 345
  • 1
  • 3
  • 10

2 Answers2

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