Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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

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

1 Answer

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.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...