Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Create your online store today with Shopify

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Disclosure: My content contains affiliate links.

43,222 questions

56,124 answers

573 users

How to sort a list of classes that contain nested classes using custom comparison logic in C#

1 Answer

0 votes
using System;
using System.Collections.Generic;
using System.Linq;
 
// ------------------------------------------------------------
// Example: Sorting a list of classes that contain nested classes
// ------------------------------------------------------------
 
// A nested class representing an address
public class Address
{
    public string City { get; set; }
    public int Zip { get; set; }
}
 
// A class representing a person, containing a nested Address
public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public Address Address { get; set; }
}
 
public class Program
{
    // ------------------------------------------------------------
    // Helper function to print the list
    // ------------------------------------------------------------
    static void PrintPersons(IEnumerable<Person> people)
    {
        foreach (var p in people) {
            Console.WriteLine($"{p.Name} | age: {p.Age} | city: {p.Address.City} | zip: {p.Address.Zip}");
        }
    }
 
    // ------------------------------------------------------------
    // Main program
    // ------------------------------------------------------------
    public static void Main()
    {
        // Create a sample list of people
        var people = new List<Person>
        {
            new Person { Name = "Alice", Age = 30, Address = new Address { City = "San Francisco", Zip = 42100 } },
            new Person { Name = "Bob",   Age = 40, Address = new Address { City = "Austin",        Zip = 32000 } },
            new Person { Name = "Carol", Age = 35, Address = new Address { City = "New York City", Zip = 42000 } },
            new Person { Name = "Dave",  Age = 25, Address = new Address { City = "Austin",        Zip = 32000 } },
            new Person { Name = "Eve",   Age = 28, Address = new Address { City = "New York City", Zip = 61000 } }
        };
 
        // ------------------------------------------------------------
        // Sort using LINQ:
        //   1. Order by city
        //   2. Then by zip
        //   3. Then by age
        // This naturally accesses nested fields and keeps the code clear.
        // ------------------------------------------------------------
        var sorted =
            people
                .OrderBy(p => p.Address.City)
                .ThenBy(p => p.Address.Zip)
                .ThenBy(p => p.Age);
 
        // Print the sorted result
        PrintPersons(sorted);
    }
}
 
 
 
/*
run:
 
Dave | age: 25 | city: Austin | zip: 32000
Bob | age: 40 | city: Austin | zip: 32000
Carol | age: 35 | city: New York City | zip: 42000
Eve | age: 28 | city: New York City | zip: 61000
Alice | age: 30 | city: San Francisco | zip: 42100
 
*/

 



answered Sep 1 by avibootz
edited Sep 1 by avibootz

Related questions

...