How to get an array of the positions of a sorted array without changing its positions.

I’m doing a AI weapon selector. Each weapon has a fitness value depending on the position of the nearest enemy. I want the AI to use the weapon with the highest fitness, but only if it is over 0.75 and the weapon has bullets left.

Here is my code.

using System.Collections;

public class AIWeaponInput : MonoBehaviour {
	
	WeaponManager wM;
	private const float DESITION_TIME = 0.2f;
	private float temp;
	
	public float viewRadius;
	public Collider[] colliders;
	public Vector3 nearestCarPos;
	public float[] fitness;
	
	private int[] sorted;
	
	
	void Start(){
		wM = GetComponent<WeaponManager>();
		fitness = new float[wM.weaponList.Count];

	}
	
	void Update(){
		temp -= Time.deltaTime;
		if(temp < 0){
			temp = DESITION_TIME;
			UpdateData();
			EvaluateFitness();
			SelectWeapon();
		}
	}
	
	
	
	
	private void UpdateData(){
		colliders = Physics.OverlapSphere(transform.position, viewRadius, 1 << Layers.CAR);
		if(colliders.Length > 1){
			nearestCarPos = colliders[1].transform.root.position;
		}
		
		
	}
	
	private void EvaluateFitness(){
		for (int i = 0; i < wM.weaponList.Count; i++) {
			fitness _= wM.weaponList*.CalculateFitness(nearestCarPos);*_

* } *
* }*

* private void SelectWeapon(){*
* //Help!*
* }*

}
Finding the one with the highest fitness its pretty easy, the thing is to find the second, or third if the ones with the better fitness don’t have bullets left.
I can’t bubble sort the fitness array because its positions are linked to the list of weapons in the weapons manager.
I thought of having an array of bools to check if I have already checked that weapon, but I don’t know how to implement it.

In my opinion, parallel arrays are evil, for exactly the reason you described. If you modify the content of one array, you have to modify the content of the parallel array, which is clumsy at best, and at worst, introduces all kinds of difficult to track bugs.

Instead, why not create a new type, WeaponNode, that contains a Weapon' and a float`, denoting its fitness value.

public class WeaponNode
{
  public Weapon weapon;
  public float fitness;
}

Your code would then read,

public WeaponNode[] fitness;

void Start(){
  wM = GetComponent<WeaponManager>();
  fitness = wM.weaponList.Select(w => new WeaponNode { weapon = w, fitness = 0.0f).ToArray();
}

private void EvaluateFitness(){
  // For each weapon, compute its fitness, then order them by descending fitness value
  fitness = wM.weaponList.Select(w => new WeaponNode { weapon = w, fitness = w.CalculateFitness(nearestCarPos)).OrderByDescending(node => node.fitness).ToArray();
       }     
    }

After EvaluateFitness() is called, your fitness array will be sorted into an array, by descending fitness amounts. Note that this code is untested, but the principle should be the same.