Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
442 views
in Technique[技术] by (71.8m points)

c# - Sort two Lists<T> together as one?

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]; } 
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Use the Array.Sort overload that takes an array of keys and an array of values, it does precisely what you want. (This method has been available since .NET 2.0.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...