1

I'm looking for an efficient way to "link" arrays/lists to each other and sort all of the lists/arrays synchronously based on one of the arrays.

For example lets say I have three lists:

List<double> Data1;
List<double> Data2;
List<double> Data3;

The items inside the lists are obviously not structurally related, but I want them to be. For instance Data1[0], Data2[0] and Data3[0] are related.

So now I want to sort the collection of the three lists based on Data1 from largest to smallest. How can I do this so that all three lists stay synchronised?

Thank you in advance for your help!

2
  • please show some source code... what have you tried ? what is not working ? Commented May 20, 2012 at 12:22
  • I have not tried anything since I don't know where to begin! I read through a lot of articles but I cannot seem to get the sort of functionality that I need. I think my requirements are pretty basic so not sure why I cannot find relevant information Commented May 20, 2012 at 12:24

3 Answers 3

5

Make the items structurally related:

public class Data {
  public double Data1 { get; set; }
  public double Data2 { get; set; }
  public double Data3 { get; set; }
}

Having a List<Data>, you can easily sort the items on one of the properties:

theList.Sort((x, y) => x.Data2.CompareTo(y.Data2));
Sign up to request clarification or add additional context in comments.

3 Comments

Hey, this will fit the best into my application. Thanks a lot!
@user1035217: This is also the best design for code maintainability.
Absolutely. If they should be structurally related, then relate them. Also, the class Data could implement IComparable as an alternative way of sorting...though I like the Lambda method here.
1

I would create a list of an anonymous type that combines the values from all three respective lists together, sort based on the field you want, and then project the sorted values back into their respective lists:

var combined =
    Enumerable.Range(0, Data1.Count)
              .Select(i => new
              {
                  Item1 = Data1[i],
                  Item2 = Data2[i],
                  Item3 = Data3[i],
              })
              .OrderBy(c => c.Item1)
              .ToList();

Data1 = combined.Select(c => c.Item1).ToList();
Data2 = combined.Select(c => c.Item2).ToList();
Data3 = combined.Select(c => c.Item3).ToList();

Comments

1

You can use Zip to tie the arrays together, and then use the projected type to order by a particular point:

List<double> Data1;
List<double> Data2;
List<double> Data3;

var orderedByDataOne = Data1
    .Zip(Data2, (one, two) => new {one, two})
    .Zip(Data3, (zipped, three) => new {zipped.one, zipped.two, three})
    .OrderBy(x => x.one)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.