logo  VB.Net - Sequencing Array Elements
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 an array ascending. (Only works for Integer, String, Double etc.) Will not work on arrays containing structure elements etc.

Private Function ArraySort(Array1() As String) As String()
 Array.Sort(Array1)
 Return Array1
End Function
  
Sort Array of Objects/Structures
The structure can contain any number of variables. You can mix String, Integer, Char etc. within the structure

Private Function ArrayOfObjectSort(Array1() As ABCD) As ABCD()
 Return Array1.OrderBy(Function(x) x.A).ToArray
End Function
'
Public Structure ABCD
 Dim A As Integer
 Dim B As Integer
 Dim C As Integer
 Dim D As Integer
End Structure
  
Sort Array of Objects with 2 or More Fields
The structure can contain any number of variables. You can mix String, Integer, Char etc. within the structure

Private Function ArrayOfObjectSort(Array1() As ABCD) As ABCD()
 Return Array1.OrderBy(Function(x) x.A).ToArray
End Function
'
Private Function ArrayOfObjectSortDescending(Array1() As ABCD) As ABCD()
 Return arrABCD.OrderByDescending(Function(x) x.A).ToArray
End Function
'
Public Structure ABCD
 Dim A As Integer
 Dim B As Integer
 Dim C As Integer
 Dim D As Integer
End Structure
  
Sort Array 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 ArrayOfObjectSort(Array1() As ABCD) As ABCD()
 Return arrABCD.OrderBy(Function(x) x.A).ThenByDescending(Function(x) x.D).ThenBy(Function(x) x.B).ToArray
End Function
'
Public Structure ABCD
 Dim A As Integer
 Dim B As Integer
 Dim C As Integer
 Dim D As Integer
End Structure
  
Sort Array of Objects (Advanced)
The array sort can incorporate 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 ArraySortAdvanced(Array1() As ABCD) As ABCD()
 arrABCD = Array.Sort(arrABCD, AddressOf CompareMyStructures)
 Return arrABCD
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 or Reversing Elements in An Array
There are a built-in function to reverse all array elements and shuffle arrays. These should work in most situations

Private Function ArrayShuffle(Array1() As ABCD) As ABCD()
 Return Array1.Shuffle.ToArray
End Function
'
Private Function ArrayReverse(Array1() As ABCD) As ABCD()
 Return Array1.Reverse.ToArray
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 .ToArray
Strange IDE/Compiler error messages could indicate that the .ToArray section of a line is missing.

DigitalDan.co.uk