2

I have an assignment for school that I'm having trouble with. The assignment is to create a console app that allows users to add, modify and view a list of movies. Each movie contains attributes like a title, a year, a director, etc. I have to use the following structs:

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's the part I'm having trouble with. I need to sort List by the movie title. I'm having trouble figuring out what the correct way to go about this is. Anyone have any advice?

peterh
  • 11,875
  • 18
  • 85
  • 108
Tyler
  • 49
  • 1
  • 2
  • Movie should not be a struct (but a class). – H H Dec 04 '10 at 23:06
  • 1
    Firstly, neither of those types should be structs. Second; since you know about List-of-T, why use an array in MovieList - in fact, why have MovieList at all? – Marc Gravell Dec 04 '10 at 23:07
  • I can't change any of those. Those are requirements of the assignment. – Tyler Dec 04 '10 at 23:08
  • are you sure this isn't a C assignment? firstly, C# doesn't have structs, and secondly sorting MovieList is a trivial one liner in C# – fearofawhackplanet Dec 04 '10 at 23:08
  • @fearof - Er, yes it does. But they mean something different. – Marc Gravell Dec 04 '10 at 23:11
  • 1
    @fearofawhackplanet C doesn't have generics or access levels, so it's probably not C. and C# does have structs. – Itsik Dec 04 '10 at 23:11
  • It's definitely trivial... for someone who isn't taking a basic logic class. I appreciate all the help. It's definitely C#, not C. – Tyler Dec 04 '10 at 23:11
  • @Tyler - with respect, then: your tutor doesn't understand c# and has brought some misconceptions with them, presumably from c/c++ where struct means something different. These are **really bad** uses of struct, in virtually every way imaginable. Sorry... – Marc Gravell Dec 04 '10 at 23:12
  • @Marc Gravell. I absolutely understand what you're saying. The assignment requires me to use the above code. I'll ask the question next class. Thank you. – Tyler Dec 04 '10 at 23:14
  • Oh, and public fields; major yeuch. – Marc Gravell Dec 04 '10 at 23:14
  • 1
    comment fail then :) weird i've been programming C# for two years and never seen struct used. guess i need to read more SO :) – fearofawhackplanet Dec 04 '10 at 23:18
  • 1
    @fearof - the good news is: that probably means you are using them correctly! it is **very** uncommon to write a `struct` in C# – Marc Gravell Dec 04 '10 at 23:20
  • 2
    seems a bit harsh that this question is being downvoted. it's not the OPs fault if his tutor gives him crap assignments. – fearofawhackplanet Dec 04 '10 at 23:20
  • I agree. I would upvote it but at 23:26 UTC, all my votes were spent many hours ago. I've starred it to upvote tomorrow ;p (I tend to vote quite a lot...) - and tomorrow is here :) – Marc Gravell Dec 04 '10 at 23:27
  • @Tyler - and if he insists he is right, please feel free to discreetely throw him my e-mail address (see profile)... – Marc Gravell Dec 05 '10 at 00:12
  • Does this answer your question? [How to Sort a List by a property in the object](https://stackoverflow.com/questions/3309188/how-to-sort-a-listt-by-a-property-in-the-object) – eocron Dec 19 '20 at 09:50

3 Answers3

2

First of all, those should not be structs, they should be classes. A structure is intended for a type that represents a single value, and is much more complicated to implement properly than a class.

You can sort the movies like this:

List.movie = List.movie.OrderBy(m => m.title).ToArray();
Guffa
  • 687,336
  • 108
  • 737
  • 1,005
  • 1
    It should be noted that this creates a *second* array that is the ordered items (leaving the old array unchanged) - and then swaps the arrays. – Marc Gravell Dec 04 '10 at 23:29
2

Since it is an array (addressed separately):

Array.Sort(List.movie,
   (a,b)=>string.Compare(a.title,b.title));
Marc Gravell
  • 1,026,079
  • 266
  • 2,566
  • 2,900
0

You need to sort the array movie. So just use any sorting method, such as Merge Sort. The decision if a movie is larger/smaller than another decide by comparing the titles.

movie[a].title.CompareTo(movie[b].title)
Itsik
  • 3,920
  • 1
  • 25
  • 37