2

I got 5 lists. One is containing the date of release and the others are the attributes of that list but seperated in multiple lists.

List<string> sortedDateList = x1.OrderBy(x => x).ToList();

This code is sorting the list with the oldest date first, like it should. But I also want to sort (sync) the other attributes list, because they need the same index as the date.

How can I realize that? I'm new to Linq-methods.

4
  • 1
    How are your other lists defined? Commented Sep 12, 2017 at 16:35
  • 3
    Why don't you store a list of objects-with-properties, as opposed to a list for each property? Commented Sep 12, 2017 at 16:37
  • 1
    You could add the .ThenBy() method before the call to .ToList() Commented Sep 12, 2017 at 16:37
  • I really love Linq, but maybe it's easier if you add a new Method to your x1 List. SortAll or sth. which handles your approach internally. So you can call x1.SortAll. Internally you can just order one by one. Commented Sep 12, 2017 at 16:57

5 Answers 5

6

You could use the .Zip() method to combine the lists as described here. You could combine them into a class or an anonymous type and then sort them.

int[] numbers = { 1, 2, 3, 4 };
string[] words = { "one", "two", "three" };

var numbersAndWords = numbers.Zip(words, (first, second) => new { Num = first, Word = second });

var sorted = numbersAndWords.OrderBy(x => x.Num).ToList();

Alternately, if you can guarantee that all the lists are of the same length (or just grab the shortest list) you could use the following instead of the .Zip() extension.

var numbersAndWords = numbers.Select((number, i) => new { Num = number, Word = words[i], Foo = myFoos[i] }); // Where myFoos is another collection.

And in the lambda combine all the items from the separate lists into an object at the same time by accessing the collection by index. (Avoids multiple use of .Zip()) Of course, if you try to access an index that is larger than the list size you will get an IndexOutOfRangeException.

Sign up to request clarification or add additional context in comments.

2 Comments

Yeah, but they've got five lists. Of course you could Zip() them all, but that would quickly become very clunky syntax.
@CodeCaster +1 I agree, which is why I followed up with a suggestion to access by index. I see you had the same idea! ;)
5

As far as I understand your question, you have different lists containing properties of certain objects. You should definitely look into storing all data into one list of a class of your making, where you consolidate all separate information into one object:

var list = new List<YourClass>
{
    new YourClass
    {
        Date = ...,
        OtherProperty = ...,
    },
    new YourClass
    {
        Date = ...,
        OtherProperty = ...,
    },
};

var ordered = list.OrderBy(o => o.Date);

But if you insist in storing different properties each in their own list, then you could to select the dates with their index, then sort that, as explained in C# Sorting list by another list:

var orderedDates = list.Select((n, index) => new { Date = n, Index = index })
                       .OrderBy(x => x.Date)
                       .ToList();

Then you can use the indexes of the sorted objects to look up the properties in the other lists, by index, or sort them on index as explained in C# Sort list while also returning the original index positions?, Sorting a list and figuring out the index, and so on.

Comments

2

It almost sounds like you want 1 list of a class.

public class MyClass{
    public string Date{get; set;} //DateTime is a better type to use for dates by the way
    public string Value2{get; set;}
    public string Value3{get; set;}
    public string Value4{get; set;}
    public string Value5{get; set;}
}

...

var sortedDateList = x1.OrderBy(x => x.Date).ToList()

Comments

2

Create an Object containing the date and attributes:

public class DateWithAttributes
{
  public string Date {get;set;}
  public Attribute Attribute1 {get;set;}
  public Attribute Attribute2 {get;set;}
...
}

List<DateWithAttributes> DateWithAttributesList = new List<DateWithAttributes>()
{
  DateWithAttribute1,
  DateWithAttribute2
}
List<DateWithAttributes> sortedDateList = DateWithAttributesList.OrderBy(x => x.date).ToList();

Comments

0

If you want to keep the lists separate, and/or create the ordered versions as separate lists, then you can concatenate the index to the dates and sort by dates, then use the sorted indexes:

var orderedIndexedDateOfReleases = dateOfReleases.Select((d, i) => new { d, i }).OrderBy(di => di.d);

var orderedDateOfReleases = orderedIndexedDateOfReleases.Select(di => di.d).ToList();
var orderedMovieNames = orderedIndexedDateOfReleases.Select(di => movieNames[di.i]).ToList();

If you don't mind the result being combined, you can create a class or use an anonymous class, and again sort by the dates:

var orderedTogether = dateOfReleases.Select((d, i) => new { dateOfRelease = d, movieName = movieNames[i] }).OrderBy(g => g.dateOfRelease).ToList();

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.