Sorting an ArrayList of Vector3’s bys their distance

I have an ArrayList of Vector3’s and want to sort them by their distance from a specified point. The closest at pos[0]

I found this thread http://forum.unity3d.com/viewtopic.php?t=16557&highlight=sort+distance and it is actually almost what I want. The problem is that I need it in C#. Since I have just jumped on to the C# train :? , I really need some help. Is there anyone that could help me out?

I figured it out :slight_smile:

private ArrayList sortByDistanceFromPoint(ArrayList __listOfPositions, Vector3 __point){
		ArrayList __distances = new ArrayList();// ArrayList af distances
		foreach(Vector3 __pos in __listOfPositions) {									
			float pos = (__pos - __point).sqrMagnitude; // Set distance
			__distances.Add(pos);
		}

		Vector3[] __builtinArrayOfPos = new Vector3[__listOfPositions.Count];
		float[] __builtinArrayOfDists = new float[__distances.Count];
		
		for(int i = 0; i < __listOfPositions.Count;++i){// Convert to builtin array
			__builtinArrayOfPos[i] = (Vector3) __listOfPositions[i];
			__builtinArrayOfDists[i] = (float) __distances[i];
		}
		
		System.Array.Sort(__builtinArrayOfDists, __builtinArrayOfPos);// Sort

		return new ArrayList(__builtinArrayOfPos);
}