- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有一个对象列表,每个对象在 3D 空间中都有一个位置。我需要按到任意点的距离对这个列表进行排序。目前我正在这样做:
_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();
但是,不幸的是,我受制于使用 Unity 的编译器,该编译器在内存分配和 LINQ/委托(delegate)方面非常糟糕。有没有办法在不使用 LINQ 或委托(delegate)的情况下对这样的列表进行排序?最好是分配很少或不分配内存的搜索,因为我需要在一帧中多次运行这个东西。
此外,将来可能还会有其他任意的搜索限制(例如,如果与该特定对象的距离大于某个对象特定的最大距离,则忽略它)
编辑:我认为我没有清楚地解释我的问题。我知道排序算法,但是,所有这些解决方案都与 2 个独立可比较的对象有关。我在问如何根据外部变量对这些对象进行排序。也就是说,它们需要根据到空间中给定点的距离进行排序,对象不知道,但排序算法知道。我知道这可以通过了解这一点的对象来完成,但这对我来说是糟糕的设计。
基本上,我需要一个 Sort() 实现,它接受一个参数以及要排序的对象,并将该参数与对象结合使用以对列表进行排序(如 LINQ 实现中所示 - 位置是参数此行所在的函数)。
最佳答案
您可以使用 List.Sort()
。但是,如果您想使用此方法,存储在列表中的对象类型应实现 IComparable
接口(interface)。下面我提供了一个代码示例,您可以基于该示例编写自己的代码:
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();
}
}
关于 List.Sort()
方法的复杂性,如 MSDN 中所述,
On average, this method is an O(n log n) operation, where n is Count; in the worst case it is an O(n ^ 2) operation.
关于c# - 在没有 LINQ 或委托(delegate)的情况下对 C# List<> 进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25054076/
我看了很多文章,但我仍然不清楚我们通常创建的普通委托(delegate)和多播委托(delegate)之间的区别。 public delegate void MyMethodHandler(objec
考虑以下几点: Action a1 = new Action(_insert); Action a2 = new Action(a1); a2 指的是什么?它是 a1,a1 的浅拷贝还是 a1 的深拷
我希望这听起来像是一个显而易见的问题,但是委托(delegate)返回类型是否也必须与其委托(delegate)的方法的返回类型相匹配? EG,像这样: public static void Save
我想使用 Kotlin 委托(delegate),但我不想在委托(delegate)人之外创建委托(delegate)。委托(delegate)的所有示例都如下所示: interface Worker
class SuperClass { var delegate : SuperClassDelegate? } protocol SuperClassDelegate { func d
我有一个加载 View 的 View ,需要将 View 推送到主导航 Controller 。 我已经为每个 View 设置了一个委托(delegate),并且基本上使我的调用沿着“链”返回到主导航
为简单起见,假设我想创建一个自定义 UITextField 并向其添加一个简单的行为;也就是说,如果文本字段成为第一响应者,背景颜色将变为绿色。 为此,在我的自定义类中,我必须将该类设置为委托(del
我非常有信心我应该能够使用非静态方法的委托(delegate),但下面给了我一个错误: public class TestClass { private delegate void TestD
在 C# 中不能从 System.Delegate 或 System.MulticastDelegate 继承。只要您声明标准的“运行时托管”方法,就完全可以在 MSIL 中执行此操作。但是,每次我向
我在 Storyboard 中定义了一个 iPad 界面,带有一个 SplitViewController。我想将 SplitViewController 的委托(delegate)设置为指向详细 C
我有几个解析器。有一个顶级的可以委托(delegate)给另一个。 Parser我们从 Reader 中获取他们的输入(可变)。我只想要一个 Parser为了能够一次解析,只有一个解析器应该有 Rea
一直以来我都在阅读关于反射的文章,每个人都在说:“反射很慢”,“反射很慢”。 现在我决定测试速度有多慢,令我惊讶的是,使用反射创建的委托(delegate)实际上是使用 lambda 创建的委托(de
在 Xcode 4.5 中启动了 Cocos2D 2.1 模板(没有物理引擎),针对 iOS 6 和 iPad。在 CDAudioManager.m 文件中,以下代码... AVAudioSessio
以下是来自未管理的 dll 的函数代码。它接受一个函数指针作为参数,并简单地返回被调用函数返回的值。 extern __declspec(dllexport) int _stdcall callDe
//NewCharts.h #import @interface NewCharts : UIViewController @property(nonatomic,retain)IBOutlet U
鉴于以下 MSDN 示例代码,为什么我不能定义 Action 委托(delegate)“内联”: public static void Main(string[] args) { Action
在虚幻引擎中,UFUNCTION用于通过附加说明符来丰富功能,以用于蓝图使用、复制和委托(delegate)。 然而,一些委托(delegate)类型似乎不允许绑定(bind) UFUNCTION(如
我刚刚将照片选择器放入我的项目中,一切正常。唯一的事情是它坚持在我设置委托(delegate)的地方给我以下警告 - Assigning to 'id' from incompatible type
我有一个 UIImageView 的子类,并且想将 self 作为参数传递给委托(delegate)。我在 MyImageView 之前收到错误“预期 ')'”。我需要将对象传递给委托(delegat
我正在开发 iOS 10 的语音转文本功能。 我希望调用 SFSpeechRecognitionTaskDelegate 的委托(delegate)方法来检查完成的结果。 func speechRec
我是一名优秀的程序员,十分优秀!