简体   繁体   English

对没有LINQ或委托的C#List <>进行排序

[英]Sorting a C# List<> without LINQ or delegates

I have a list of objects, each of which have a position in 3D space. 我有一个对象列表,每个对象在3D空间中都有一个位置。 I need to sort this list by distance to an arbitrary point. 我需要按到任意点的距离排序此列表。 Currently I'm doing this with: 目前,我正在这样做:

_attachedEffectors = _attachedEffectors.OrderBy(x =>
        Mathf.Pow((x.transform.position.x - position.x), 2) + Mathf.Pow((x.transform.position.y - position.y), 2) + Mathf.Pow((x.transform.position.z - position.z), 2)
        ).ToList();

However, unfortunately I'm constrained by using Unity's compiler which is horrible with memory allocation and LINQ/delegates. 但是,不幸的是,我受到使用Unity的编译器的束缚,该编译器对内存分配和LINQ /代理很可怕。 Is there any way to sort a list like this without using LINQ or delegates? 有没有办法在不使用LINQ或委托的情况下对这样的列表进行排序? Optimally a search that allocates little or no memory as I need to run this thing many times a frame. 最好是一次分配很少或没有内存的搜索,因为我需要在一帧中多次运行此东西。

Also in future there may be other, arbitrary constraints on the search (eg if the distance from this particular object is more than some object-specific maximum distance, ignore it) 将来在搜索上可能还会有其他任意约束(例如,如果距此特定对象的距离大于某个特定于对象的最大距离,请忽略它)

EDIT: I don't think I clearly explained my problem. 编辑:我不认为我清楚地解释了我的问题。 I'm aware of sorting algorithms, however, all these solutions are regarding 2 independantly comparable objects. 我知道排序算法,但是,所有这些解决方案都是关于2个独立的可比较对象。 I'm asking how to sort these objects in regards to an external variable . 我在问如何根据外部变量对这些对象进行排序。 That is, they need to be sorted by distance to a given point in space that the objects don't know about, but the sorting algorithm does. 也就是说,需要根据到对象知道的空间中给定点的距离对它们进行排序,但是排序算法可以对它们进行排序。 I know this could be done by the objects knowing about this point but that screams bad design to me. 我知道可以通过了解这一点的对象来完成此操作,但这使我感到不满意。

Basically, I need a Sort() implementation that takes a parameter as well as the objects to be sorted, and uses that parameter in conjunction with the objects to sort the list (as seen in the LINQ implementation - position is parameter to the function this line is in). 基本上,我需要一个Sort()实现,该实现带有一个参数以及要排序的对象,并将该参数与对象一起使用以对列表进行排序(如LINQ实现中所示-position是该函数的参数)行中)。

You could use the List.Sort() . 您可以使用List.Sort() However if you want to use this method, the object's type that is stored in your list should implement the IComparable interface. 但是,如果要使用此方法,则存储在列表中的对象类型应实现IComparable接口。 Below I provide you a sample of code, on which you can based on and write your own: 下面,我为您提供示例代码,您可以基于这些代码并编写自己的代码:

public class Customer : IComparable<Customer>
{
    public int Age { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public Customer(int age, string firstName, string lastName)
    {
        Age = age;
        FirstName = firstName;
        LastName = lastName;
    }

    public int CompareTo(Customer other)
    {
        return Age.CompareTo(other.Age);
    }
}

class Program
{
    static void Main(string[] args)
    {
        List<Customer> customers = new List<Customer>
        {
            new Customer(25,"a","b"),
            new Customer(21,"c","d"),
            new Customer(22,"e","f"),
            new Customer(28,"g","i"),
            new Customer(30,"j","k"),
            new Customer(23,"l","m"),
            new Customer(31,"a","b"),
        };


        customers.Sort();

        foreach (var customer in customers)
        {
            Console.WriteLine(customer.Age);
        }

        Console.ReadKey();
    }
}

Regarding the complexity of List.Sort() method, as it is stated in MSDN , 关于List.Sort()方法的复杂性,如MSDN中所述

On average, this method is an O(n log n) operation, where n is Count; 平均而言,此方法是O(n log n)运算,其中n是Count; in the worst case it is an O(n ^ 2) operation. 在最坏的情况下,它是O(n ^ 2)运算。

Have a look at this table on Wikipedia. 在Wikipedia上查看表。 It gives you an overview of many common sorting algorithms and how they perform against others. 它概述了许多常见的排序算法以及它们与其他算法相比的性能。 For your purposes you can look at the ones who need O(1) memory. 为了您的目的,您可以看看那些需要O(1)内存的对象。 Just pick an algorithm and happy coding! 只需选择一种算法并编写愉快的代码即可!

Something like an Insertion sort or Heapsort should suffice for your needs, depending it it has to be stable or not. 诸如插入排序或堆排序之类的东西应足以满足您的需求,这取决于它是否稳定。

I have not used unity but you might be able to use something like this 我没有用过团结,但您也许可以使用这样的东西

alist.sort(delegate(someObject o1, someObject o2) {
    // Compare property1
    int compare1 = o1.proprty1.CompareTo(o2.proprty1);
    if (compare1 != 0)
        return compare1;
    // Compare property2
    return o1.proprty2.CompareTo(o2.proprty2);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM