C # - sorting a list inside a structure

Hey guys. I have a school assignment with which I have problems. The purpose is to create a console application that allows users to add, modify and view a list of films. Each film contains attributes such as title, year, director, etc. I should use the following structures:

struct Movie{
    public string title;
    public string year;
    public string director;
    public float quality;
    public string mpaaRating;
    public string genre;
    public List<string> cast;
    public List<string> quotes;
    public List<string> keywords;
}
struct MovieList{
    public int length;
    public Movie[] movie;
}
...
MovieList List = new MovieList()

Here is the part I'm having problems with. I need to sort the list by movie title. It’s hard for me to figure out how to do it right. Anyone have any tips?

+3
source share
3 answers

, , . , , .

:

List.movie = List.movie.OrderBy(m => m.title).ToArray();
+2

( ):

Array.Sort(List.movie,
   (a,b)=>string.Compare(a.title,b.title));
+2

You need to sort the array movie. So just use any sorting method, for example Merge Sort. Deciding if the movie is larger / smaller than the other decides by comparing the titles.

movie[a].title.CompareTo(movie[b].title)
0
source

Source: https://habr.com/ru/post/1778538/


All Articles