排序两个列表< T>合而为一? [英] Sort two Lists<T> together as one?

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

问题描述

我有两个表,我感到索引访问(如名称[10] 日期[10] )。他们紧密联系在一起,使名称[10] 是与日期[10] ,反之亦然。

这个设计一直运作良好,但现在我需要对它们进行排序。我可以分别对它们进行排序,但两者显然这将消除两个列表之间的关系,使他们毫无价值。

我想过使用词典<字符串,日期和GT; 来代替,但似乎我只能通过密钥(这也是不可行的)访问。基本上我需要三样东西,两个值和我可以通过数字迭代一个索引(不是的foreach )。

谁能帮助?看来我要么需要改变数据结构或制定出如何排序两种截然不同的列表< T> 一起...

 公共类结果
{
公开名单<串GT;名称{;组; }
公开名单<&日期时间GT;日期{搞定;组; }
}为(中间体X = 0; X<结果; X ++)
{z.N = results.Name [X]; z.D = results.Date [X]; }


解决方案

这听起来像你对抗做明显的解决方案其中涉及的的前期的工作位:创建一个类来封装名称/日期对。

这是的绝对的你应该做的。然后你就可以有一个列表,通过名称或日期或任何你想对它进行排序,你永远不必担心这两个列表不同步等。

如果你的结果类并没有做任何事情,除了包含这两个名单,你其实可以用相同数量的类型结束了 - 你可以沟结果完全,有利于列表与LT的;结果> ,其中结果有名称和日期。

I have two List which I am accessing by index (e.g. Name[10], Date[10]). They're closely tied, so Name[10] is related to Date[10] and vice versa.

This design has worked well but now I need to sort them. I can sort them both individually but obviously that would remove the relationship between the two lists and make them worthless.

I thought about using a Dictionary<string, DateTime> instead but it seems I can only access that by Key (which is also unworkable). Essentially I need three things, two values and one index which I can iterate through numerically (not foreach).

Can anyone help? It seems I either need to change data-structure or work out how to sort two distinct List<T> together...

public class Results
{ 
public List<string> Name { get; set; }
public List<DateTime> Date{ get; set; }
}

for (int x = 0; x < results; x++)
{ z.N = results.Name[X]; z.D = results.Date[x]; } 

解决方案

It sounds like you're fighting against doing the obvious solution which involves a little bit of upfront work: creating a type to encapsulate the name/date pair.

That's absolutely what you should do. Then you can have one list, sort it by name or by date or whatever you want, and you never need to worry about the two lists getting out of sync etc.

If your Results class doesn't do anything apart from contain those two lists, you can actually end up with the same number of types - you can ditch Results entirely, in favour of List<Result> where Result has a name and a date.

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

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