简体   繁体   English

如何为排序过程提供比较和赋值运算符

[英]How to provide comparison and assignment operators for sorting procedure

I have to arrays with this structure 我必须使用这种结构的数组

internal sealed class Task
{
    ...
    public String Id { get; private set; }
    public String Name { get; private set; }
    public UInt32 Time { get; private set; }
}
...
    private List<Task> _tasks;
    private List<Boolean[]> _tasksAllowedProcessingUnits;

This arrays have one-to-one correspondence. 此数组具有一一对应关系。 I need to sort array _tasks but while swapping elements in array _tasks i need elements in _tasksAllowedProcessingUnits to be swapped as well. 我需要数组进行排序_tasks但同时在交换阵列元件_tasks我需要在元件_tasksAllowedProcessingUnits被交换为好。 So, one-to-one correspondence must be preserved. 因此,必须保留一对一的对应关系。

For some performance reasons i don't want to put all task related data in a struct to store it in a single array. 由于某些性能原因,我不想将所有与任务相关的数据放在一个结构中以将其存储在单个数组中。

There isn't anything pre-done. 没有什么事要做。 Clearly there are various solutions 显然有各种解决方案

A) You find the source of an implementation of QuickSort and you do the Sort + Sort "manually" with that code A)您找到了实现QuickSort的源,并使用该代码“手动”进行了排序+排序

B) You create an overobject that references Task and Boolean[] and sort a collection of that. B)创建一个引用TaskBoolean[]并对它们的集合进行排序。

List<Tuple<Task, Boolean[]>> lst = Enumerable.Range(0, _tasks.Count).Select(ix => Tuple.Create(_tasks[ix], _tasksAllowedProcessingUnits[ix])).ToList();
lst.Sort((p, q) => p.Item1.CompareTo(q.Item1));

for (int i = 0; i < _tasks.Count; i++) {
    _tasks[i] = lst[i].Item1;
    _tasksAllowedProcessingUnits[i] = lst[i].Item2
}

(note that the .NET Task object isn't comparable, so I hope you have a specialized comparer) (请注意,.NET Task对象不可比,因此希望您有一个专门的比较器)

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

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