How do I swap gameObjects on a list by clicking in the UI?

I’m making a game where the player moves a certain piece by rearranging “movement cards” in order to create the path he wants the piece to take to reach the end goal. The problem is, I don’t know how to do the rearranging part.

Currently, I have a UI element that has a fixed amount of Images that change sprite according to the list information and are given a random order and I’d like to know how I could make the player rearrange said pieces by clicking or dragging on the images and swapping them with the other images [example in the image]. (Swap those two pieces)
[175483-help.png*_|175483]
Here’s the code I have right now, and I honestly have no idea where to go from here. I’m very new to programming and I’ve been teaching myself only through experimenting so please don’t go too hard on me :stuck_out_tongue:

void DarPeçasNecessarias() //Gives the exact amount of necessary pieces to finish the level
    {
        if (direçao.x > 0)
        {
            numRight = (int) direçao.x;
            for (int i = 0; i < numRight; i++)
            {
                Peçass.Add("Right");
            }
            if (direçao.y > 0)
            {
                numUp = (int) direçao.y;
                for (int i = numRight; i < numRight + numUp; i++)
                {
                    Peçass.Add("Up");
                }
            }
            else if (direçao.y < 0)
            {
                numDwn = (int) - direçao.y;
                for (int i = numRight; i < numRight + numDwn; i++)
                {
                    Peçass.Add("Down");
                }
            }
            else { numDwn = 0; numUp = 0; }

        }
        else if (direçao.x < 0)
        {
            numLft = (int) - direçao.x;
            for (int i = 0; i < numLft; i++)
            {
                Peçass.Add("Left");
            }
            if (direçao.y > 0)
            {
                numUp = (int) direçao.y;
                for (int i = numLft; i < numLft + numUp; i++)
                {
                    Peçass.Add("Up");
                }
            }
            else if (direçao.y < 0)
            {
                numDwn = (int) - direçao.y;
                for (int i = numLft; i < numLft + numDwn; i++)
                {
                    Peçass.Add("Down");
                }
            }
            else { numDwn = 0; numUp = 0; }
        }
        else { numLft = 0; numRight = 0; }
        //-----------------
        numPeçasNecessarias = numRight + numLft + numUp + numDwn;
        DarPeçasExtra();
    }

    void DarPeçasExtra() //Gives extra pieces
    {
        for (int i = numPeçasNecessarias; i < NumPeças; i++)
        {
            int nrg = Random.Range(1,5);
            //Debug.Log(nrg);
            if (nrg == 1)
            {
                Peçass.Add("Up");
                numUp++;
            }
            else if (nrg == 2)
            {
                Peçass.Add("Down");
                numDwn++;
            }
            else if (nrg == 3)
            {
                Peçass.Add("Left");
                numLft++;
            }
            else
            {
                Peçass.Add("Right");
                numRight++;
            }
        }

        ShuffleList();
    }

    void ShuffleList() //Shuffles the List
    {
        string Temp = "Temp";
        for (int i = 0; i < Peçass.Count; i++)
        {
            int rnd = Random.Range(0, Peçass.Count);
            Temp = Peçass[rnd];
            Peçass[rnd] = Peçass*;*

Peçass = Temp;
}
FillImages();
}

private void Update()
{
if (Input.GetKeyDown(KeyCode.P))
{
canMove = !canMove;
}

if (canMove)
{
if (!isRunning)
{
StartCoroutine(AplicarMovimento());
}

}else { StopCoroutine(AplicarMovimento()); isRunning = false; }

AbrirHotbar();
FillImages();
}

void AbrirHotbar()//Opens the “hotbar” where all the movement cards are located
{
if (Input.GetMouseButtonDown(0))
{
Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y);
RaycastHit2D hit = Physics2D.Raycast(mousePos2D, Vector2.zero);

if (hit.transform.gameObject.CompareTag(“TilePeça”) && !Hotbar.activeSelf)
{
Hotbar.SetActive(true);
}
else if (hit.transform.gameObject.CompareTag(“TilePeça”) && Hotbar.activeSelf)
{
Hotbar.SetActive(false);
}
}
}

void FillImages() // Changes the sprite of the images in the Hotbar
{
for (int i = 0; i < Peçass.Count; i++)
{
Transform botao = Hotbar.transform.GetChild(i);

if (Peçass == “Up”)
{
botao.GetComponent().sprite = Up;
}else if (Peçass == “Down”)
{
botao.GetComponent().sprite = Down;
}
else if (Peçass == “Left”)
{
botao.GetComponent().sprite = Left;
}
else if (Peçass == “Right”)
{
botao.GetComponent().sprite = Right;
}
}
}
_*

Get the list index of both the source object and a temp variable, then swap them by index.

List<Object> list = new List<Object>();
int target, source; //index of target and source object
Object tmp; //temporary store the target object

void SwapObjects()
{
	tmp = list[target];
	list[target] = list[source];
	list[source] = tmp;
}