Comparing variables of each element in a list with each other

Hey there, so I have multiple checkpoint objects in a level. What I want is for the player to have his x position changed to the x position of the checkpoint object with the largest x value - in other words, as the player activates checkpoints around the level, it takes the checkpoint with the largest x as the most recent checkpoint and moves the player there if he dies.

If I were to put the checkpoints into a list (if this is the best way to do it) how would I compared the x positions between all of them and get the value of the one with the largest x value and apply it to a variable? Here’s what I’ve been fiddling with to see if maybe it will help give you an idea:

    public class CheckpointSetter : MonoBehaviour 
{
	public float checkpointX;
	public float checkpointY;

	void Start ()
	{
		GetLastCheckpoint.checkpoints.Add(this);
	}

	void OnTriggerEnter2D(Collider2D other)
	{
		if (other.gameObject.tag == "Player")
		{
			checkpointX = transform.position.x;
			checkpointY = transform.position.y;
		}
	}

}

This script takes the x and y position of the gameobject and stores it, while also adding that checkpoint object to a list.

public class GetLastCheckpoint : MonoBehaviour 
{
	public static List<CheckpointSetter> checkpoints = new List<CheckpointSetter>();
	public float playerRespawnX;
	public float playerRespawnY;

	void Start () 
	{
		
	}

	public void GetLatestCheckpoint()
	{
		
	}

	//playerRespawnX =
	//playerRespawnY = 

This script makes the list, is to perform logic to find the checkpoint object with the greatest checkpointx value, and then apply it to the playerRespawnX variable, which is then applied to the player transform if he dies. Any help would be great, thank you.

You could just bite the bullet and implement IComparable. Then you could maintain your list in a sorted order.

The other option is to use a foreach loop as follows

private CheckpointSetter GetHighestX(){
    float bestX = -999;
    CheckpointSetter bestCheckPoint;
    foreach (CheckpointSetter checkpoint in checkpoints){
        if (checkpoint.x > bestX){
            bestX = checkpoint.x;
            bestCheckPoint = checkpoint;
        }
    }
    return bestCheckPoint;
}

Perhaps try something like this:

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

public class Checkpoint : MonoBehaviour {

	[SerializeField] List<GameObject> checkpoints = new List<GameObject>();
	private int lastCheckpointActivated = 0;
	[HideInInspector] public Vector3 spawnPoint;

	void Awake(){
		spawnPoint = checkpoints[0].transform.localPosition;
	}

	public void ActivateCheckpoint(){
		Vector3[] allCheckpoints = new Vector3[checkpoints.Count];

		for(int i = 0; i < checkpoints.Count; i++){
			allCheckpoints _= checkpoints*.transform.localPosition;*_

* }*

* spawnPoint = checkpoints[HighestValue(checkpoints)].transform.localPosition;*
* lastCheckpointActivated = checkpoints[HighestValue(checkpoints)];*
* }*

* int HighestValue (List cp_list){
float max = cp_list[0].transform.localPosition.x;
_
int locationInList = 0;_
for (int i = 0; i < cp_list.Count; i++) {
if(cp_list.transform.localPosition.x > max){
max = cp_list.transform.localPosition.x;
_ locationInList = i;
}
}
return locationInList;
}
}*_