是否有一个基于索引排序C#支持? [英] Is there C# support for an index-based sort?

查看:108
本文介绍了是否有一个基于索引排序C#支持?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何内置的做一个索引排序支持C#

更​​多细节:结果
我拥有多套存储在双个别泛型列表数据。
这些名单总是长度相等,并持有相应的数据项,但这些列表来动态去,所以我不能只在相对应的类的数据项存储或结构干净。 (我也在处理一些历史遗留问题。)

More Details:
I have several sets of data stored in individual generic Lists of double. These are lists always equal in length, and hold corresponding data items, but these lists come and go dynamically, so I can't just store corresponding data items in a class or struct cleanly. (I'm also dealing with some legacy issues.)

我需要能够进行排序从这些数据集的任何一个关键字。

I need to be able to sort these keyed from any one of the data sets.

我要做到这一点的最好办法想到的就是增加一个间接层,并使用基于索引的排序。 。这样的排序已经使用了多年

My thought of the best way to do this is to add one level of indirection, and use an index based sort. Such sorts have been in use for years.

快速定义基于排序的的结果
进行指数,连续整数数组长度相同的列表,然后排序算法排序整数列表,以便 anylist [指数[N] 给出anylist在第N项有序。这份名单本身永远都不会重新排序。

Quick definition of index based sort :
make "index", an array of consecutive integers the same length as the lists, then the sort algorithm sorts the list of integers so that anylist[index[N]] gives the Nth item of anylist in sorted order. The lists themselves are never re-ordered.

有没有内置的做一个索引排序C#支持?
我一直无法找到它......一切,我已经找到了重新排序集合本身。我的猜测是支持存在,但我还没有在正确的地方看着呢。

Is there any built-in C# support for doing an index sort? I have been unable to find it... everything I have found reorders the collection itself. My guess is support exists but I haven't looked in the right place yet.

我使用C#.NET 3.5,Windows下。

I am using C#.NET 3.5, under Windows.

推荐答案

一旦你建立了数组索引,您可以使用自定义它排序比较< T> 这些值在相应项目的数据数组中进行比较:

Once you have set up the index array, you can sort it using a custom Comparison<T> that compares the values in the corresponding items in the data array:

Array.Sort<int>(index, (a,b) => anylist[a].CompareTo(anylist[b]));

这篇关于是否有一个基于索引排序C#支持?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆