Sort two Lists 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]; }

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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.

Method 3

Do yourself a favor and keep the two things together:

public class NameAndDate {
  public string Name { get; set; }
  public DateTime Date { get; set; }
}

Then you keep them in one list (List<NameAndDate>):

If you want to sort by the name, add a IComparer implementation to NameAndDate, then you can just call Sort() on the list.

If you want to keep access to the Name and the Date, add accessor methods to Results, like

public string GetName(int index) {
  return list[i].Name;
}

Method 4

List<Tuple<string,DateTime> NameDateList {get; set; }

Is an easy way to accomplish what you want, but for clarity you are probably better off making a custom type to house them such as:

public class NameDate
{
    public string Name { get; set; }
    public DateTime Date{ get; set; }
}

Then you could use:

List<NameDate> NameDateList {get; set; }

Method 5

You could create a custom type to hold a value, or if you only need to use it in the scope of a single method you could use an anonymous type as follows.

var namesAndDates = MyNames
    .Select((name, i) => new {name, date = MyDates[i]});

From here the two values are no longer loosely tied. Now you can sort them.

var sortedNamesAndDates = namesAndDates.OrderBy(a => a.date);

Or whatever else you’ll need to do with them.

Method 6

Create a class called “Result” with two properties

public class Result
{
  public string Name {set;get;}
  public DateTime Date { set;get;}
}

Now you may create a List of this class

List<Result> objListResult=new List<Result>();

Now in your case, If you want, you may read the content of your indidual List and put it to our new List. (This is only purticular to your case, if you really want to move data from your old Lists to our new List. If possible you may update the code where you load data to two List(that is the correct way) so that it will load data to our new List of Type Result and avoid this porting code to move data to new List.

 for(int i=0;i<Name.Count;i++)
 {
   objListResult.Add(new Result { Name =Name[i].ToString(), Date[i].ToString() });
 }

You can access the Items from the new List like this

 objListResult[2].Date

will give you the Date Stored for the second object in the list

 objListResult[2].Name

will give you the Name Stored for the second object in the list

Method 7

you can use dictionary with Key Name , and value Date

and U can after that sort them easily


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x