Sometimes we’ll want to sort a collection by something other than its natural order. For example, suppose we wanted to sort strings by their length instead of alphabetically. Here’s an example of custom sorts in Visual Basic .NET.
To run the program, save it as SortingByFunctions.vb and use the VB.NET compiler:
In this Visual Basic .NET example, we’ve used Array.Sort with a custom Comparison(Of String) delegate to sort the fruits array by string length. For sorting custom objects, we’ve used LINQ’s OrderBy method, which provides a more idiomatic approach in .NET for sorting collections.
The Person class is defined as a nested class within the module for simplicity, but in a real-world scenario, you might want to define it in a separate file.
Note that Visual Basic .NET uses different syntax for anonymous functions (lambda expressions) and type declarations compared to other languages. The Function(a, b) syntax is used for lambda expressions, and type declarations come after variable names.