简体   繁体   中英

C# List.OrderBy with multiple lists

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.

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 .

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.

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

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();

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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM