Zhambul
Zhambul

Reputation: 942

how to sort list of object? OrderBy c#

I have Track class

class Track
{
    public String Name { get; set; }
    public Double Duration;
    public Boolean IsRemix;

    public override string ToString()
    {
        if (IsRemix) return Name + "(Remix) " + Duration;
        return Name + " " + Duration;
    }
}

and Album is a List:

class Album :List<Track>
    {
        public Album SortByAlphabet()
        {
            return new Album().OrderBy(x=>x.Name);
        }
    }

How can I order Album by Track's name? In SortByAlphabet I need only Album be output.

Edit

return this.OrderBy(x=>x.Name).ToList();

and

return (Album)this.OrderBy(x=>x.Name).ToList();

doesn't help

it's
throwing an exception, InvalidCastException

Upvotes: 1

Views: 184

Answers (3)

jdphenix
jdphenix

Reputation: 15445

Don't inherit from List<T>. You aren't making a list, you're making an album.

An album has a list of songs, and you can represent this with a member. It uses your implementation of Track (I'd honestly not use Boolean, Double, etc. and not have bare public fields, but that's another discussion.)

class Album {
  private readonly List<Track> _tracklist;
  public ReadOnlyCollection<Track> Tracklist {
    get {
      return new ReadOnlyCollection<Track>(_tracklist);
    }
 }

  public Album(IEnumerable<Track> tracklist) {
    _tracklist = new List<Track>(tracklist);
  }
}

Usage:

var album = new Album(new List<Track> {
  new Track { Name = "Blonde"}, 
  new Track { Name = "K.-O."}, 
  new Track { Name="Alcaline"}, 
  new Track { Name="Seulement pour te plaire"}, 
  new Track { Name="L'amour renfort"},
  new Track { Name="Bi"},
  new Track { Name="Mon planeur"},
  new Track { Name="Ce qui tue l'amour"},
  new Track { Name="Tweet"},
  new Track { Name="Charles est stone"},
  new Track { Name="Mylène Farmer"},
  new Track { Name="Plus de bye bye"}
});

var culture = new CultureInfo("fr-FR");
var tracks = album.Tracklist.OrderBy(t => t.Name, StringComparer.Create(culture, false));
tracks.ToList().ForEach(Console.WriteLine);

Output:

Alcaline 0
Bi 0
Blonde 0
Ce qui tue l'amour 0
Charles est stone 0
K.-O. 0
L'amour renfort 0
Mon planeur 0
Mylène Farmer 0
Plus de bye bye 0
Seulement pour te plaire 0
Tweet 0

Upvotes: 2

dbc
dbc

Reputation: 117275

I would not suggest inheriting from List, but if you do, the following should meet your requirement:

class Album : List<Track>
{
    public Album() : base() { }

    public Album(IEnumerable<Track> tracks) : base(tracks) { }

    /// <summary>
    /// Sort in place the album of tracks alphabetically by name 
    /// </summary>
    public void SortByAlphabet()
    {
        Sort((t1, t2) => t1.Name.CompareTo(t2.Name));
    }

    /// <summary>
    /// Return a new Album with tracks sorted alphabetically by name.
    /// </summary>
    /// <returns></returns>
    public Album OrderByAlphabet()
    {
        return new Album(this.OrderBy(t => t.Name));
    }
}

Upvotes: 2

Reza Abolfathi
Reza Abolfathi

Reputation: 3191

try this

class Album : List<Track>
{
    public Album SortByAlphabet()
    {
        return (Album)(this.OrderBy(o => o.Name).ToList());
    }
}

Upvotes: -2

Related Questions