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 structs that contain nested structs using custom comparison logic in VB.NET

1 Answer

0 votes
Imports System
Imports System.Collections.Generic
Imports System.Linq
 
' ------------------------------------------------------------
' Example: Sorting a list of user-defined structures
' that contain nested structures
' ------------------------------------------------------------
 
' A nested structure representing an address
Public Structure Address
    Public City As String
    Public Zip As Integer
End Structure
 
' A structure representing a person, containing a nested Address
Public Structure Person
    Public Name As String
    Public Age As Integer
    Public Address As Address
End Structure
 
Module Program
 
    ' ------------------------------------------------------------
    ' Helper function to print the list
    ' ------------------------------------------------------------
    Sub PrintPersons(people As IEnumerable(Of Person))
        For Each p In people
            Console.WriteLine($"{p.Name} | age: {p.Age} | city: {p.Address.City} | zip: {p.Address.Zip}")
        Next
    End Sub
 
    ' ------------------------------------------------------------
    ' Main program
    ' ------------------------------------------------------------
    Sub Main()
        ' Create a sample list of people
        Dim people As New List(Of Person) From {
            New Person With {.Name = "Alice", .Age = 30,
                             .Address = New Address With {.City = "San Francisco", .Zip = 42100}},
            New Person With {.Name = "Bob",   .Age = 40,
                             .Address = New Address With {.City = "Austin",        .Zip = 32000}},
            New Person With {.Name = "Carol", .Age = 35,
                             .Address = New Address With {.City = "New York City", .Zip = 42000}},
            New Person With {.Name = "Dave",  .Age = 25,
                             .Address = New Address With {.City = "Austin",        .Zip = 32000}},
            New Person With {.Name = "Eve",   .Age = 28,
                             .Address = New Address With {.City = "New York City", .Zip = 61000}}
        }
 
        ' ------------------------------------------------------------
        ' Sort using LINQ:
        '   1. Order by city
        '   2. Then by zip
        '   3. Then by age
        ' This mirrors the C++ comparator logic cleanly.
        ' ------------------------------------------------------------
        Dim sorted =
            people _
                .OrderBy(Function(p) p.Address.City) _
                .ThenBy(Function(p) p.Address.Zip) _
                .ThenBy(Function(p) p.Age)
 
        ' Print the sorted result
        PrintPersons(sorted)
    End Sub
 
End Module
 
 
' 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

...