1

I have some objects. And in each object there is a propety age. And I want to sort the property age of all the objects. And in this case I have two objects. But for example if you have three, four..etc objects. Maybe there is a more generic method for this?

So this is the code:

List<Person> personList = new List<Person>
{

    new Person { Age = 77 },
    new Person { Age = 88 },
    new Person { Age = 1001 },
    new Person { Age = 755 }
};

List<Dog> dogList = new List<Dog>
{

    new Dog { Age = 1 },
    new Dog { Age = 9 },
    new Dog { Age = 10 },
    new Dog { Age = 8 }
};

personList.Sort((x, y) => x.Age.CompareTo(y.Age));
dogList.Sort((x, y) => x.Age.CompareTo(y.Age));

So the output has to be in this case:

1 8 9 10 77 88 755 1001

2
  • You can use quicksort or simply comparison based on ur required value and swap items. Just search for geeksforgeeks quicksort or google Commented Oct 7, 2021 at 11:55
  • Do you just want to sort each list individually? Or get a single sorted list of all Age values from each list? Commented Oct 7, 2021 at 11:57

2 Answers 2

3

In addition to Christian's approach of mutating / extending an existing list with Concat, it's also possible to do this without materializing the projections and using Union to produce a new combined sequence, before ordering, like so:

var sortedAges = personList
    .Select(p => p.Age)
    .Union(dogList.Select(d => d.Age))
    .OrderBy(age => age);

foreach (var age in sortedAges) // << The result is only 'materialized' here, once.
{ ...
Sign up to request clarification or add additional context in comments.

2 Comments

it's also possible to do this without materializing the projections - can you please give other readers (including me) a link to read up on this. Without adding a new question I'm interested in the way algorithms and data types are chosen depending on the LINQ. Thanks
Good point. I can't find an absolutely 'definitive' guide to lazy evaluation / aka deferred execution (suggest google around), but essentially IEnumerable and IQueryable enable 'stream' type processing, as opposed to concrete in memory processing (like a List, Array etc). There are pros and cons of either approach, but its always good to really know what's going on under the hood, especially when dealing with large data sources and also IQueryable sources like Entity Framework / ORMs, where materialization will actually execute a query on the database.
2

You could create two lists of the ages which will create an IEnumerable<int>, and Combine and Order them...

var personAsAge = personList.Select(p => p.Age);
var dogAsAge = Dogist.Select(d => d.Age);

var ageOrder = personAsAge.Concat(dogAsAge).OrderBy(a => a);

You will need to import System.Linq; too.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.