Sort speed list

Hi, I make a turn-based game, and in the start of game all of characters sending from their scripts a message to Fight-Manager Script, and Figth-Manager Script has sort the speedList and next it sending an message to the fastest character. But, I dont know what do this… I know that the standart list have a void Sort() but later i must send back to the fastest character a permission to move.

This is my test sort script:

it have two lists, and it have to sort both, so that the “first” of theList2 was on the same index as the biggest int from theList. This is only example and it not work because some values may be the same and i dont know what to do. Please help.

PS. Sorry for my english

it would probably be better to store the “scripts” in the list rather than just the speed numbers, if you include “System.Linq” namespace you can then sort the list by their member attributes using the syntax demonstrated below:

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

public class FightManager : MonoBehaviour
{
  
    public List<Fighter> fighters;


    void Start ()
    {
        fighters = GameObject.FindObjectsOfType<Fighter>().ToList(); // grab all the fighter scripts in the scene
        StartCoroutine(TakeTurn());
    }
  
    IEnumerator TakeTurn()
    {
        fighters = fighters.OrderBy(f => f.speed).ToList(); // sort the list by their "speed" attribute

        Debug.Log("Turn start");
        for (int i=0;i<fighters.Count;i++)
        {
            StartCoroutine(fighters[i].Attack());
            yield return new WaitForSeconds(1f);
        }
        Debug.Log("Turn over");
    }

}
using UnityEngine;
using System.Collections;

public class Fighter : MonoBehaviour
{
    public FightManager fightManager;
    public float speed;

    void Awake ()
    {
        speed = Random.Range(0f, 10f);
        fightManager = FindObjectOfType<FightManager>();
    }
  

    public IEnumerator Attack()
    {
        Debug.Log(gameObject.name + " attacked");
        yield return new WaitForSeconds(1f);
    }
}

I use Bubble Sort to do this:
http://pastebin.com/Z0My6Unc
It do job, how do you think about it?

Bubblesort is not… terrible but use the build in List.Sort for lists.

I writed in the thread that i cant use it, because i must sort two lists, one of int type, one of my script type, and bubblesort is good in this case.

you seem very attached to this approach… why do you have to have the two lists?

I advise this instead:

System.Collections.Generic.SortedList<float>

LINQ comes at a huge performance cost for some platforms. Why don’t you just use a SortedList? Add objects based on their speed and get the last object (because its key which is its speed would be greatest). This completely eliminates the need for lookups or LINQ.

1 Like

didn’t know it existed… to the docs! thanks :slight_smile:

Yup. The .NET framework has so many useful tools like this.

Can you show me that how SortedList will be look in my case? Thanks!

Thank you too, you show me the better way :slight_smile: