简体   繁体   中英

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:

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.

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

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:

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

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