-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?

2

2 Answers 2

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);
    }
  }
Sign up to request clarification or add additional context in comments.

Comments

0

Order by ascending :

 var newList = YourObj.OrderBy(x => x.Price).ToList();

Order by Descending:

  var newList = YourObj.OrderByDescending(x => x.Price).ToList();

1 Comment

If you want to answer question with plenty of duplicates at least provide an answer. List<T> is not array.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.