How to sort list of gameObject base on it's int variable

I’m making a reload system similar on escape from tarkov, but after a week of experimenting, debugging etc i’m still struggling to make it work. I’m new in game dev in fact this is my 1st project

this is my logic:

i have this script attached to my magazine prefab so that weapons ammo depends on the magazine ammo count.

public class MagazineAmmo : MonoBehaviour
{
    public int magAmmo = 12;
    public int currentAmmo;

    void Start()
    {
        currentAmmo = magAmmo;
    }
}

i also have vest/bulletproof vest prefab with magazine pouches that has magazines prefab in it as a child, this vest is instantiated on player’s body.

now i want to access those magazines with higher magAmmo so i could transfer that to the weapon, this is what i’ve done so far

public class MagazineOnVest : MonoBehaviour
{
    public string searchTag;
    public List<GameObject> rifleMags = new List<GameObject>();
    public List<MagazineAmmo> magAmmoCount = new List<MagazineAmmo>();
    public List<int> ammoCount = new List<int>();

    void Start()
    {

    }

    void Update()
    {


        if (searchTag != null)
        {
            FindObjectwithTag(searchTag);

        }
    }

    public void FindObjectwithTag(string _tag)
    {
        rifleMags.Clear();
        magAmmoCount.Clear();
        ammoCount.Clear();
        Transform parent = transform;
        GetChildObject(parent, _tag);
    }

    public void GetChildObject(Transform parent, string _tag)
    {
        for (int i = 0; i < parent.childCount; i++)
        {
            Transform child = parent.GetChild(i);
            MagazineAmmo mags = child.GetComponent<MagazineAmmo>();
            if (child.tag == _tag)
            {
                rifleMags.Add(child.gameObject);
                magAmmoCount.Add(mags);
                ammoCount.Add(mags.magAmmo);
                if (ammoCount != null)
                {
                    ammoCount.Sort();
                }



            }
            if (child.childCount > 0)
            {
                GetChildObject(child, _tag);
            }
        }
    }

this script is attached on vest prefab, getting the magazine with higher ammo is where i am currently struggeling.

if anyone has a better logic for this pls help me, i am really out of ideas :frowning: thanks…

Sorting and finding the object with the highest value are two different things.

Here is my suggested NOT TESTED rework

public class MagazineOnVest : MonoBehaviour
{
    public string searchTag;

    void Update()
    {
        if (searchTag != null)
        {
            MagazineAmmo magazineAmmo = FindMagazineWithHighestAmmunitions(transform, searchTag);
            // Do something with magazineAmmo
        }
    }

    public MagazineAmmo FindMagazineWithHighestAmmunitions(Transform parent, string searchTag)
    {
        MagazineAmmo magazineWithHighestAmmunitions = null;
        for (int childIndex = 0; childIndex < parent.childCount; childIndex++)
        {
            Transform child = parent.GetChild(childIndex);
            if (child.CompareTag(searchTag))
            {
                magazineWithHighestAmmunitions =
                    HasHighestAmmunition(magazineWithHighestAmmunitions, child.GetComponent<MagazineAmmo>());
            }
            if (child.childCount > 0)
            {
                magazineWithHighestAmmunitions =
                    HasHighestAmmunition(magazineWithHighestAmmunitions, FindMagazineWithHighestAmmunitions(child, searchTag));
            }
        }

        return magazineWithHighestAmmunitions;
    }

    private Magazine HasHighestAmmunition(Magazine first, Magazine second)
    {
        if(first == null)
            return second;

        if(second == null)
            return first;

        return first.magAmmo >= second.magAmmo ? first : second;
    }
}

thanks man this code work as intended and its clean…