Confusion over LINQ syntax

I’m trying to use LINQ’s OrderBy to organize a list by distance, such that the following function will always organize myList[0] to be the closest gameobject to myObject:

void SortObjectsByDistance(List<GameObject> myList, GameObject myObject){
	myList = (List<GameObject>)myList.OrderBy (myObject => (myObject.transform.position - transform.position).sqrMagnitude);
}

However, this produces an error which makes me think my syntax is off: “a local variable named ‘myObject’ cannot be declared in this scope because it would give a different meaning to ‘myObject’, which is already used in a parent or current scope to denote something else”. There’s nothing called myObject outside of this function, which makes me think I’m using LINQ wrong- do I need to make a third, junk variable and assign it to myObject somehow?

myList = myList
.OrderBy(myObject=>Vector3.Distance(myObject.transform.position,transform.position)
.ToList();

EDIT: I removed one set of () around the parameter of Distance as there were two sets. Her below is a demo program that is tested and working:

List<GameObject> myList = new List<GameObject>();
void Start () 
{
    GameObject objA = new GameObject();
    objA.transform.position = new Vector3(5,0,0);
    GameObject objB = new GameObject();
    objB.transform.position = new Vector3(2,0,0);
    GameObject objC = new GameObject();
    objC.transform.position = new Vector3(7,0,0);
    GameObject objD = new GameObject();
    objD.transform.position = new Vector3(6,0,0);
    GameObject objE = new GameObject();
    objE.transform.position = new Vector3(1,0,0);

    myList = new List<GameObject>(){objA, objB, objC, objD, objE };

    myList = myList.OrderBy(myObject => Vector3.Distance(myObject.transform.position,transform.position)).ToList();
}

There’s nothing called myObject outside of this function”. No, but there is already a (unnecessary) local parameter called myObject specified in the function signature, which is accessible from within the lambda’s scope and therefore conflicts with the declaration of myObject there.

I also think you’ll have problems with your approach of casting the result back to your List. Try this instead:

void SortObjectsByDistance(List<GameObject> myList){
    myList = myList.OrderBy(myObject => (myObject.transform.position - transform.position).sqrMagnitude).ToList();
}