排序一个列表<>根据另一 [英] Sort one List<> based on another

查看:138
本文介绍了排序一个列表<>根据另一的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

说我有

List<int> ages  = new List<int>() { 8, 5, 3, 9, 2, 1, 7 };
List<int> marks = new List<int>() { 12, 17, 08, 15, 19, 02, 11 };



我可以通过<$我的标记排序C $ C>年龄是这样的:

I can sort my marks by ages like this:

while (true)
{
  bool swapped = false;

  for (int i = 0; i < ages.Count - 1; i++)
    if (ages[i] > ages[i + 1])
    {
      int tmp = ages[i];
      ages[i] = ages[i + 1];
      ages[i + 1] = tmp;

      tmp = marks[i];
      marks[i] = marks[i + 1];
      marks[i + 1] = tmp;

      swapped = true;
    }

  if (!swapped)
    break;
}

现在我想把这到接受任何两个列表的功能。第一个参数将是参考列表,数值或类似列表。第二个参数将包含数据的列表

Now I want to put this into a function that accepts any two lists. The first parameter will be the reference list, the numerical or comparable list. The second parameter will be the list containing the data.

例如:

public static void Sort<T>(List<T> RefList, List<T> DataList)
{
  // sorting logic here...
}

有几个问题:

首先, T 是几乎可以肯定不是在同一类型 RefList 的DataList 。 RefList可能是日期,整数或双打;而DataList控件可以自由是绝对什么。我需要能够接收两种,任意泛型类型

First of all, T is almost certainly not the same type in RefList and DataList. RefList might be dates, integers, or doubles; whereas DataList is free to be absolutely anything. I need to be able to receive two, arbitrary generic types.

其次,我似乎无法使用> 运营商与 T 在这一行:

Secondly, I cannot seem to use the > operator with the T in this line:

if (ages[i] > ages[i + 1])

也许我的整个做法是错误的。

Perhaps my whole approach is wrong.

顺便说一句,我读给建议,两个列表应合并成一个复合数据类型的一个列表类似的问题的回答。这不是为我的应用程序的实用性。所有我想要做的是写的基础上的另一种元素,不知怎的,排序一个列表的静态函数。

By the way, I have read responses to similar questions that suggest that the two lists should be combined into a single list of a compound data type. This isn't practical at all for my application. All I want to do is write a static function that somehow sorts one list based on the elements of another.

推荐答案

要排序1列出你希望你的实际需要以某种方式保持从项目的引用在第一个列表他们体重/键在第二个列表的方式。没有现成的方法做到这一点,你可以使用任意值不容易准的元数据(例如,如果一个列表是 INT 的列表,你的情况有什么映射到键第二个列表)。你唯一合理的选择是2列出在同一时间进行排序,并通过指数的关联 - 再没有现成的课程,以帮助

To sort one list the way you want you actually need to somehow keep references from items in first list to they weight/keys in the second list. No existing methods do that as you can't easily associate metadata with arbitrary values (i.e. if first list is list of int as in your case there is nothing to map to keys in second list). Your only reasonable option is to sort 2 lists at the same time and make association by index - again no existing classes to help.

这可能是更容易使用的解决方案,你拒绝。即简单的ZIP和排序依据,不是重新第一个列表:

It may be much easier to use solution that you reject. I.e. simply Zip and OrderBy, than recreate first list:

ages = ages
  .Zip(marks, (a,m)=> new {age = a; mark = m;})
  .OrderBy(v => v.mark)
  .Select(v=>v.age)
  .ToList();

请注意(phoog提供):如果你需要做这种类型的使用Array有排序的< A HREF =htt​​p://msdn.microsoft.com/en-us/library/85y6y2d3%28v=vs.100%29.aspx相对=nofollow>的Array.Sort 允许正是这种operatiion (见phoog的详情答案)。

Note (courtesy of phoog): if you need to do this type of sorting with Array there is Array.Sort that allows exactly this operatiion (see phoog's answer for details).

这篇关于排序一个列表&LT;&GT;根据另一的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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