How to Shuffle a List in C# and VB.NET

To Shuffle a List in C# and VB.NET you can use the snippet below.

Sample C#

public static IList<T> ShuffleIList<T>(IList<T> inputList)
{
	var cryptoServiceProvider = new RNGCryptoServiceProvider();
	var count = inputList.Count;
	while (count > 1)
	{
		var bytes = new byte[1];
		do cryptoServiceProvider.GetBytes(bytes);
		while (!(bytes[0] < count * (Byte.MaxValue / count)));
		var index = (bytes[0] % count);
		count--;
		var input = inputList[index];
		inputList[index] = inputList[count];
		inputList[count] = input;
	}
	return inputList;
}

Sample VB.NET

Public Shared Function ShuffleIList(Of T)(inputList As IList(Of T)) As IList(Of T)
	Dim cryptoServiceProvider = New RNGCryptoServiceProvider()
	Dim count = inputList.Count
	While count > 1
		Dim bytes = New Byte(0) {}
		Do
			cryptoServiceProvider.GetBytes(bytes)
		Loop While Not (bytes(0) < count * ([Byte].MaxValue / count))
		Dim index = (bytes(0) Mod count)
		count -= 1
		Dim input = inputList(index)
		inputList(index) = inputList(count)
		inputList(count) = input
	End While
	Return inputList
End Function

How to order a dictionary by Value in C# and VB.NET

To order/sort a dictionary by Value in C# and VB.NET you can use the snippet below.

Sample C#

MyDict = MyDict.OrderBy(x => x.Value).ToDictionary(x => x.Key, x => x.Value);

Sample VB.NET

MyDict = MyDict.OrderBy(Function(x) x.Value).ToDictionary(Function(x) x.Key, Function(x) x.Value)

How to Bubblesort ILists in C# and VB.NET

To Bubblesort ILists in C# and VB.NET you can use the follwing snippet.

Sample C#

public static IList Sort(IList inputArray)
{
	var counter = inputArray.Count - 1;
	for (var i = 0; i < counter; i++)
	{

		for (var index = counter; index > i; index--)
		{
			if (((IComparable)inputArray[index - 1]).CompareTo(inputArray[index]) <= 0) continue;
			var temp = inputArray[index - 1];
			inputArray[index - 1] = inputArray[index];
			inputArray[index] = temp;
		}
	}
	return inputArray;
}

Sample VB.NET

Public Shared Function Sort(inputArray As IList) As IList
	Dim counter = inputArray.Count - 1
	For i As var = 0 To counter - 1

		For index As var = counter To i + 1 Step -1
			If DirectCast(inputArray(index - 1), IComparable).CompareTo(inputArray(index)) <= 0 Then
				Continue For
			End If
			Dim temp = inputArray(index - 1)
			inputArray(index - 1) = inputArray(index)
			inputArray(index) = temp
		Next
	Next
	Return inputArray
End Function

How to sort a collection in VBA

To sort a collection in VBA you can use the following snippet.
This snippet is using bubblesort.

Sample VBA

Public Function SortCollection(colInput As Collection) As Collection
Dim iCounter As Integer
Dim iCounter2 As Integer
Dim temp As Variant

Set SortCollection = New Collection
For iCounter = 1 To colInput.Count - 1
    For iCounter2 = iCounter + 1 To colInput.Count
        If colInput(iCounter) > colInput(iCounter2) Then
           temp = colInput(iCounter2)
           colInput.Remove iCounter2
           colInput.Add temp, temp, iCounter
        End If
    Next iCounter2
Next iCounter
Set SortCollection = colInput
End Function

for more informations see Bubble sort

How to quicksort in PHP

To quicksort in PHP you can sue the following snippet.

Sample PHP

function Sort_Quicksort($input) 
{
    if(!count($input)) return $input;
    $data= $input[0];
    $small = $big = array();
    $length = count($input);
    for($i=1; $i < $length; $i++) {
        if($input[$i] <= $data) {
            $small [] = $input[$i];
        } else {
            $big[] = $input[$i];
        }
    }
    return array_merge(Sort_Quicksort($small), array($data), Sort_Quicksort($big));
}