Sorting a list by distance to an object?

Hi. I think similar questions have been answered already, but I have had a hard time understanding those answers.

Basically, I want to sort a list of transforms by distance to the player, so the closest transform is at the top and of the list and farthest is at the bottom. I don’t use lists that often so I’m not too familiar with their sort functions or how they work. A sample script or explanation would be greatly appreciated.

Thanks.

This ended up working for me, never used linq or OrderBy before so I’m still not too sure why this works but it does. If someone has a better method or can explain this method, I’d be happy to hear it.

   using UnityEngine;
    using System.Collections;
    using System.Linq;
    
    public class SortDistance : MonoBehaviour {
    
        public GameObject[] points;
        public GameObject sphere;
    
        void Start()
        {
            points = points.OrderBy(point => Vector3.Distance(sphere.transform.position, point.transform.position)).ToArray();
    
            foreach (GameObject point in points)
                Debug.Log(point.name);
        }
    }

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class SortDistance : MonoBehaviour
{

    public GameObject sphere;
    public List<Transform> spawnPoints;

    void Start()
    {
        Sort();
    }

    void Sort()
    {
        spawnPoints.Sort(delegate (Transform a, Transform b)
      {
          return Vector3.Distance(sphere.transform.position, a.transform.position).CompareTo(Vector3.Distance(sphere.transform.position, b.transform.position));
      });

        foreach (Transform point in spawnPoints)
            Debug.Log(point.name);
    }

}

This method also seems to work. Taken from sort transforms by distance to player - Questions & Answers - Unity Discussions
If anyone has the same problem.

I don’t usually use delegates but thanks to your example, it has helped me to improve the Sort I had.
Basically I want to share the following.
I was looking for a way to order the list by distance, but indicating the objective.
Until now I had a class variable and didn’t need more.
But I had the inconvenience that I needed to order the list inside a for loop and indicate the target and know who was closest to the target.
The following code is the one that helps me do that:

using System.Collections.Generic;
using UnityEngine;

public class Test_script: MonoBehaviour{
	//	───── VARIABLES: External objects ──────────
	[Header("--- External objects ---")]
	public	Transform	objectExample;

	[Space(20)]
	public List<Transform> ListExample = new List<Transform>();
	
	//	───── FUNCTIONS: Events ────────────────
	// Sort list when I click the button.
	public void ClickMeToSort(){
		// Sort the list and indicate in order of closest to the object that is passed as an argument
		// In may case is a GameObject
		SortByObject(this.objectExample);
	}

	//	───── FUNCTIONS: Special ──────────────── (Sort, Order, estroy, ...)
	/**	<summary>
			Sort list by distance to target.
		</summary>
		
		<param name="target">Distance to this target.</param>
	*/
	private void SortByObject(Transform target){
		this.ListExample.Sort(delegate(Transform t1, Transform t2){
			return
				Vector3.Distance(
					t1.position,
					target.position
				)
				
				.CompareTo(
					Vector3.Distance(
						t2.position,
						target.position
					)
				)
			;
		});
	}
}

External variables: These are the tests I did with a couple of objects.
I made a quick list and assign buckets to the example list
198395-script.jpg
Once I click on a button the function ClickMeToSort() is executed
After clicking, this would be the result: