简体   繁体   English

如何对具有多个变量的 class 列表进行多级排序

[英]How to do multi-level sort on a List of a class with multiple variables

I have a class called Movies with the following variables declared:我有一个名为 Movies 的 class 声明了以下变量:

string Title;
string Plot;
string MPAA;
string Certification;
string[] Genres;

I create a List of Movies with the following code:我使用以下代码创建了一个电影列表:

Movie m = new Movie(strTitle, strPlot, strMPAA, strCertification, strArrGenres);
MovieList.Add(m);

I'm now trying to figure out the best way to sort the list.我现在正试图找出对列表进行排序的最佳方法。 I need to do two sorts, the first is a simple Sort by Title.我需要做两种排序,第一种是简单的按标题排序。 I attempted to use LINQ, but I can't figure out how to access the variable within the Movie correctly.我尝试使用 LINQ,但我不知道如何正确访问电影中的变量。

The second one will be more tricky.第二个会更棘手。 I need to sort by Genre THEN Title.我需要按类型然后标题排序。 Each movie of course can have multiple Genres, and I know I will end up with multiple movies since the movie will be in each Genre.当然,每部电影都可以有多个流派,而且我知道我最终会制作多部电影,因为这部电影将属于每个流派。

MovieList.OrderBy(m => m.Title)

and

MovieList.OrderBy(m => m.Genre).ThenBy(m => m.Title)

should do it.应该这样做。

Use .OrderByDescending() and .ThenByDescending() if you want either sort to be descending如果您希望任一排序为降序,请使用.OrderByDescending().ThenByDescending()

I did not understand how you want to sort by Genres if they are contained inside the movies, maybe you want to filter by genre and then sort by title?如果电影中包含流派,我不明白你想如何按流派排序,也许你想按流派过滤,然后按标题排序?

    class Movie
    {
        public string Title { get; set; }
        public string[] Genres { get; set; }
    }
    static void Main(string[] args)
    {
        var movies = new List<Movie>();
        movies.Add(new Movie { Title = "Pulp Fiction", Genres = new string[] { "Crime", "Thriller" } });
        movies.Add(new Movie { Title = "Back to the Future", Genres = new string[] { "Adventure", "Sci-Fi" } });
        movies.Add(new Movie { Title = "The Dark Knight", Genres = new string[] { "Action", "Crime" } });

        var byTitle = from m in movies orderby m.Title select m;

        var crimeMovies = from m in movies where m.Genres.Contains("Crime") orderby m.Title select m;
    }

EDIT: Selecting movies with genre and ordering by Genre then Title (as per comment):编辑:选择具有流派的电影并按流派排序,然后按标题(根据评论):

        var distinctGenres = from m in movies
                             from genre in m.Genres
                             group genre by genre into genres
                             select genres.First();                          

        var moviesWithGenre = from g in distinctGenres
                              from m in movies
                              where m.Genres.Contains(g)
                              orderby g, m.Title
                              select new { Genre = g, Movie = m };

        foreach (var m in moviesWithGenre)
        {
            Console.WriteLine("Genre: "+ m.Genre + " - " + m.Movie.Title);
        }

Output: Output:

Genre: Action - The Dark Knight
Genre: Adventure - Back to the Future
Genre: Crime - Pulp Fiction
Genre: Crime - The Dark Knight
Genre: Sci-Fi - Back to the Future
Genre: Thriller - Pulp Fiction

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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