logo  VB.Net - Sequencing Elements in Lists
This Chapter
Arrays
Lists
Chapters
Home Page
Colours, RGB
Computer Specifications
Dates&Times
Disk Drives
Files
Folders
GPS and OS Ref
VB.Net Forms
Image Files
If & Select
List/Array
Mathematics
NuGet
Sound
String Functions
Sun and Moon
User Controls
Validation
DigitalDan Sites
My Other Sites
Contact Site

Note
Some pages
may contain
inaccuracies
Hits=5
Simple Sort
Sort a list ascending. (Only works for Integer, String, Double etc.) Will not work on lists containing structure elements etc. Subtitute all occurences of String for the required data type.

Private Function SortList_Simple(list1 As List(Of String)) As List(Of String)
 list1.Sort()
 Return list1
End Function
  
Sort List Of Object
The structure can contain any number of variables. You can mix String, Integer, Char etc. within the structure. You can replace x with any variable name not already in use.

Private Function SortListOfObject(ListABCD As List(Of ABCD)) As List(Of ABCD)
 Return ListABCD.OrderBy(Function(x) x.A).ToList
End Function
'
Private Function SortListOfObjectDescending(ListABCD As List(Of ABCD)) As List(Of ABCD)
 Return ListABCD.OrderByDescending(Function(x) x.A).ToList
End Function
'
Public Structure ABCD
 Dim A As Integer
 Dim B As Integer
 Dim C As Integer
 Dim D As Integer
End Structure
  
Sort List of Objects using multiple Fields
The structure can contain any number of variables. You can mix String, Integer, Char etc. within the structure. You can mix ascending/descending options within the sort fields. Sort fields can be of different types.

Private Function SortListOfObject_MultipleSortFields(ListABCD As List(Of ABCD)) As List(Of ABCD)
 Return ListABCD.OrderBy(Function(x) x.A).ThenByDescending(Function(x) x.D).ThenBy(Function(x) x.B).ToList
End Function
'
Public Structure ABCD
 Dim A As Integer
 Dim B As Integer
 Dim C As Integer
 Dim D As Integer
End Structure
  
Sort List of Objects (Advanced)
This List sort incorporates a separate function to create customised sort patterns. In this example, we will sort on field .A thenBy the last digit in field .B
This works because the compare operator returns 0 when a match is encountered

Private Function SortListOfObject_Advanced(ListABCD As List(Of ABCD)) As List(Of ABCD)
 ListABCD.Sort(AddressOf CompareMyStructures)
 Return ListABCD
End Function
'
Public Function CompareMyStructures(x As ABCD, y As ABCD) As Integer
 'Compare by Value in A first.
 Dim result As Integer = x.A.CompareTo(y.A)
 If result = 0 Then
  ' .A values are the same so compare by .B_Mod_10
  result = (x.B Mod 10).CompareTo(y.B Mod 10)
 End If
 Return result
End Function
'
Public Structure ABCD
 Dim A As Integer
 Dim B As Integer
 Dim C As Integer
 Dim D As Integer
End Structure
  
Shuffling Elements in a List
There is a built-in function to shuffle lists. This should work in most situations

Private Function ListShuffle(ListABCD(As List(of ABCD)) As List(of ABCD)
 Return ListABCD.Shuffle.ToList
End Function
'
Public Structure ABCD
 Dim A As Integer
 Dim B As Integer
 Dim C As Integer
 Dim D As Integer
End Structure
  
Reversing Elements in a List
There are a built-in function to reverse all entries in a list. This should work in most situations

Private Function ListReverse(ListABCD(As List(of ABCD)) As List(of ABCD)
 Return ListABCD.Reverse.List
End Function
'
Public Structure ABCD
 Dim A As Integer
 Dim B As Integer
 Dim C As Integer
 Dim D As Integer
End Structure
  
Hint
Many of the above functions have lines ending .ToList
Strange IDE/Compiler error messages could indicate that the .ToList section of a line is missing.

DigitalDan.co.uk