Arrays and Lists are used frequently in programming. These range from simple Arrays and Lists such as Integer Arrays, or a List(Of String), to more advanced Arrays or Lists of custom classes. Often, you'll need to sort these, either ASC (ascending) or DESC (descending). There are a variety of different ways to sort your collections, some of which sort the collection 'in place', some which create a new ordered Array or List.
This is used to sort an Integer or String Array, either ASC or DESC. The Arrays that are passed to these sorter methods are randomly sorted Integer or String example Arrays or Lists for the first eight examples, and Arrays or Lists of the Star class after that.
Public Shared Sub sorterOne(ByVal ASC As Boolean, intArray() As Integer, strArray() As String) 'simple Array.Sort If strArray Is Nothing Then Console.WriteLine("Random Array: " & String.Join(", ", intArray)) Array.Sort(intArray) If Not ASC Then Array.Reverse(intArray) End If Console.WriteLine("Sorted Array: " & String.Join(", ", intArray)) Else Console.WriteLine("Random Array: " & String.Join(", ", strArray)) Array.Sort(strArray) If Not ASC Then Array.Reverse(strArray) End If Console.WriteLine("Sorted Array: " & String.Join(", ", strArray)) End If End Sub
Public Shared Sub sorterTwo(ByVal ASC As Boolean, intArray() As Integer, strArray() As String) 'simple LINQ sort If strArray Is Nothing Then Console.WriteLine("Random Array: " & String.Join(", ", intArray)) intArray = intArray.OrderBy(Function(i) i).ToArray If Not ASC Then intArray = intArray.OrderByDescending(Function(i) i).ToArray End If Console.WriteLine("Sorted Array: " & String.Join(", ", intArray)) Else Console.WriteLine("Random Array: " & String.Join(", ", strArray)) strArray = strArray.OrderBy(Function(s) s).ToArray If Not ASC Then strArray = strArray.OrderByDescending(Function(s) s).ToArray End If Console.WriteLine("Sorted Array: " & String.Join(", ", strArray)) End If End Sub
Public Shared Sub sorterThree(ByVal ASC As Boolean, intArray() As Integer, strArray() As String) 'simple LINQ sort - Method 2 If strArray Is Nothing Then Console.WriteLine("Random Array: " & String.Join(", ", intArray)) Array.Sort(intArray, Function(x, y) If(ASC, x.CompareTo(y), y.CompareTo(x))) Console.WriteLine("Sorted Array: " & String.Join(", ", intArray)) Else Console.WriteLine("Random Array: " & String.Join(", ", strArray)) Array.Sort(strArray, Function(x, y) If(ASC, x.CompareTo(y), y.CompareTo(x))) Console.WriteLine("Sorted Array: " & String.Join(", ", strArray)) End If End Sub
Public Shared Sub sorterFour(ByVal ASC As Boolean, intArray() As Integer, strArray() As String) 'simple Bubble sort If strArray Is Nothing Then Console.WriteLine("Random Array: " & String.Join(", ", intArray)) For outer As Integer = 0 To intArray.GetUpperBound(0) For inner As Integer = 0 To outer - 1 If If(ASC, intArray(inner), intArray(outer)) > If(ASC, intArray(outer), intArray(inner)) Then Dim temp As Integer = intArray(inner) intArray(inner) = intArray(outer) intArray(outer) = temp End If Next Next Console.WriteLine("Sorted Array: " & String.Join(", ", intArray)) Else Console.WriteLine("Random Array: " & String.Join(", ", strArray)) For outer As Integer = 0 To strArray.GetUpperBound(0) For inner As Integer = 0 To outer - 1 If If(ASC, strArray(inner), strArray(outer)) > If(ASC, strArray(outer), strArray(inner)) Then Dim temp As String = strArray(inner) strArray(inner) = strArray(outer) strArray(outer) = temp End If Next Next Console.WriteLine("Sorted Array: " & String.Join(", ", strArray)) End If End Sub
Public Shared Sub sorterFive(ByVal ASC As Boolean, intList As List(Of Integer), strList As List(Of String)) 'simple List.Sort If strList Is Nothing Then Console.WriteLine("Random List: " & String.Join(", ", intList)) intList.Sort() If Not ASC Then intList.Reverse() End If Console.WriteLine("Sorted List: " & String.Join(", ", intList)) Else Console.WriteLine("Random List: " & String.Join(", ", strList)) strList.Sort() If Not ASC Then strList.Reverse() End If Console.WriteLine("Sorted List: " & String.Join(", ", strList)) End If End Sub
Public Shared Sub sorterSix(ByVal ASC As Boolean, intList As List(Of Integer), strList As List(Of String)) 'simple LINQ sort If strList Is Nothing Then Console.WriteLine("Random List: " & String.Join(", ", intList)) intList = intList.OrderBy(Function(i) i).ToList If Not ASC Then intList = intList.OrderByDescending(Function(i) i).ToList End If Console.WriteLine("Sorted List: " & String.Join(", ", intList)) Else Console.WriteLine("Random List: " & String.Join(", ", strList)) strList = strList.OrderBy(Function(s) s).ToList If Not ASC Then strList = strList.OrderByDescending(Function(s) s).ToList End If Console.WriteLine("Sorted List: " & String.Join(", ", strList)) End If End Sub
Public Shared Sub sorterSeven(ByVal ASC As Boolean, intList As List(Of Integer), strList As List(Of String)) 'simple LINQ sort - Method 2 If strList Is Nothing Then Console.WriteLine("Random List: " & String.Join(", ", intList)) intList.Sort(Function(x, y) If(ASC, x.CompareTo(y), y.CompareTo(x))) Console.WriteLine("Sorted List: " & String.Join(", ", intList)) Else Console.WriteLine("Random List: " & String.Join(", ", strList)) strList.Sort(Function(x, y) If(ASC, x.CompareTo(y), y.CompareTo(x))) Console.WriteLine("Sorted List: " & String.Join(", ", strList)) End If End Sub
Public Shared Sub sorterEight(ByVal ASC As Boolean, intList As List(Of Integer), strList As List(Of String)) 'simple Bubble sort If strList Is Nothing Then Console.WriteLine("Random List: " & String.Join(", ", intList)) For outer As Integer = 0 To intList.Count - 1 For inner As Integer = 0 To outer - 1 If If(ASC, intList(inner), intList(outer)) > If(ASC, intList(outer), intList(inner)) Then Dim temp As Integer = intList(inner) intList(inner) = intList(outer) intList(outer) = temp End If Next Next Console.WriteLine("Sorted List: " & String.Join(", ", intList)) Else Console.WriteLine("Random List: " & String.Join(", ", strList)) For outer As Integer = 0 To strList.Count - 1 For inner As Integer = 0 To outer - 1 If If(ASC, strList(inner), strList(outer)) > If(ASC, strList(outer), strList(inner)) Then Dim temp As String = strList(inner) strList(inner) = strList(outer) strList(outer) = temp End If Next Next Console.WriteLine("Sorted List: " & String.Join(", ", strList)) End If End Sub
Public Shared Sub sorterNine(ByVal ASC As Boolean, starArray() As Star) 'LINQ Object Array sort Console.WriteLine("Random Array: " & String.Join("; ", Array.ConvertAll(starArray, Function(s) s.ToString))) If ASC Then starArray = starArray.OrderBy(Function(s) s.lastName).ThenBy(Function(s) s.firstName).ToArray Else starArray = starArray.OrderByDescending(Function(s) s.lastName).ThenByDescending(Function(s) s.firstName).ToArray End If Console.WriteLine("Sorted Array: " & String.Join("; ", Array.ConvertAll(starArray, Function(s) s.ToString))) End Sub
Public Shared Sub sorterTen(ByVal ASC As Boolean, starArray() As Star) 'IComparer Object Array sort Console.WriteLine("Random Array: " & String.Join("; ", Array.ConvertAll(starArray, Function(s) s.ToString))) Dim newArray() As Star = DirectCast(starArray.Clone, Star()) Array.Sort(newArray, New Comparer(ASC)) Console.WriteLine("Sorted Array: " & String.Join("; ", Array.ConvertAll(newArray, Function(s) s.ToString))) End Sub
This is the IComparer used in this method...
Public Class Comparer Implements IComparer Private ASC As Boolean Public Sub New(ASC As Boolean) Me.ASC = ASC End Sub Public Function Compare(x As Object, y As Object) As Integer Implements IComparer.Compare Dim xs As Star Dim ys As Star If ASC Then xs = DirectCast(x, Star) ys = DirectCast(y, Star) Else xs = DirectCast(y, Star) ys = DirectCast(x, Star) End If If xs.lastName.CompareTo(ys.lastName) = 0 Then Return xs.firstName.CompareTo(ys.firstName) Else Return xs.lastName.CompareTo(ys.lastName) End If End Function End Class
Public Shared Sub sorterEleven(ByVal ASC As Boolean, starArray() As Star) 'Object Array Bubble sort Console.WriteLine("Random Array: " & String.Join(", ", Array.ConvertAll(starArray, Function(s) s.ToString))) Dim newArray() As Star = DirectCast(starArray.Clone, Star()) For outer As Integer = 0 To newArray.GetUpperBound(0) For inner As Integer = 0 To outer - 1 Dim x As Star = If(ASC, newArray(inner), newArray(outer)) Dim y As Star = If(ASC, newArray(outer), newArray(inner)) If x.lastName.CompareTo(y.lastName) = 0 Then If x.firstName.CompareTo(y.firstName) > 0 Then Dim temp As Star = newArray(inner) newArray(inner) = newArray(outer) newArray(outer) = temp End If ElseIf x.lastName.CompareTo(y.lastName) > 0 Then Dim temp2 As Star = newArray(inner) newArray(inner) = newArray(outer) newArray(outer) = temp2 End If Next Next Console.WriteLine("Sorted Array: " & String.Join(", ", Array.ConvertAll(newArray, Function(s) s.ToString))) End Sub
Public Shared Sub sorterTwelve(ByVal ASC As Boolean, starList As List(Of Star)) 'LINQ Object List sort Console.WriteLine("Random List: " & String.Join("; ", starList)) If ASC Then starList = starList.OrderBy(Function(s) s.lastName).ThenBy(Function(s) s.firstName).ToList Else starList = starList.OrderByDescending(Function(s) s.lastName).ThenByDescending(Function(s) s.firstName).ToList End If Console.WriteLine("Sorted List: " & String.Join("; ", starList)) End Sub
Public Shared Sub sorterThirteen(ByVal ASC As Boolean, starList As List(Of Star)) 'Object List IComparer sort Console.WriteLine("Random List: " & String.Join("; ", starList)) Dim newList As New List(Of Star) newList.AddRange(starList.ToArray) newList.Sort(New Comparer2(ASC)) Console.WriteLine("Sorted List: " & String.Join("; ", newList)) End Sub
This is the IComparer used in this method...
Imports System.Diagnostics.CodeAnalysis Public Class Comparer2 Implements IComparer(Of Star) Private ASC As Boolean Public Sub New(ASC As Boolean) Me.ASC = ASC End Sub Public Function Compare(<AllowNull> x As Star, <AllowNull> y As Star) As Integer Implements IComparer(Of Star).Compare Dim xs As Star Dim ys As Star If ASC Then xs = x ys = y Else xs = y ys = x End If If xs.lastName.CompareTo(ys.lastName) = 0 Then Return xs.firstName.CompareTo(ys.firstName) Else Return xs.lastName.CompareTo(ys.lastName) End If End Function End Class
Public Shared Sub sorterFourteen(ByVal ASC As Boolean, starList As List(Of Star)) 'Object List Bubble sort Console.WriteLine("Random List: " & String.Join("; ", starList)) Dim newList As New List(Of Star) newList.AddRange(starList.ToArray) For outer As Integer = 0 To newList.Count - 1 For inner As Integer = 0 To outer - 1 Dim x As Star = If(ASC, newList(inner), newList(outer)) Dim y As Star = If(ASC, newList(outer), newList(inner)) If x.lastName.CompareTo(y.lastName) = 0 Then If x.firstName.CompareTo(y.firstName) > 0 Then Dim temp As Star = newList(inner) newList(inner) = newList(outer) newList(outer) = temp End If ElseIf x.lastName.CompareTo(y.lastName) > 0 Then Dim temp2 As Star = newList(inner) newList(inner) = newList(outer) newList(outer) = temp2 End If Next Next Console.WriteLine("Sorted List: " & String.Join("; ", newList)) End Sub