-6

Hi guys i need your help. I have a task at uni about sorting a dictionary. It's properties are :

Dictionary<string, Book> nameAndDate = new Dictionary<string, Book>(); Where the class Book has properties :

 class Book
{
    public string NAME;
    public string Date;
    public DateTime DateAsDate;
    public Book(string bookName , string date,DateTime dateAsDate)
    {
        this.NAME = bookName;
        this.Date = date;
        this.DateAsDate = dateAsDate;
    }
}

So my question is how can I sort the keys by ascending so that when i give the books a name and a date it would write them like this

HP1 -> 26.06.1997
HP7 -> 21.07.2007
AC -> 20.11.2009
Marto99
  • 59
  • 6
  • 2
    @BossRoss the question you link is for `Python`, which may be quite different from the question's `C#` tag. – Matt Hogan-Jones Jun 14 '18 at 08:34
  • 1
    What is the key of your dictionary? – MakePeaceGreatAgain Jun 14 '18 at 08:38
  • You cannot sort a Dictionary, or sort a SortedDictionary by anything but its Key. You can obtain a sorted (anyway you want) list of values from a Dictionary. Because `.Values` gives you a very ordinary list. You did not specify what you actually expect here. – bommelding Jun 14 '18 at 08:49
  • The name of the book and its value is the date ,the whole point is when you add some names and dates it is added in a dictionary and then you print them in ascending order by value , I just don't get how i can do it when the values are dates – Marto99 Jun 14 '18 at 08:54
  • In fact a dictionary is fast because they use hashes instead of the actual keys in order to find an item. Ordering based on a hash is quite usless and **you should never rely on your dictionary being sorted**. If you need this use a `SortedDictionary` instead. – MakePeaceGreatAgain Jun 14 '18 at 08:57

1 Answers1

0

You can use LINQ to order your dictonnary the way you want. For example, in the following code I sort your books dictionary by date then by name so that I have the same output:

using System;
using System.Linq;
using System.Collections.Generic;

namespace ConsoleApp5
{
class Book
{
    public string Name;
    public DateTime Date;
    public Book(string bookName, DateTime dateAsDate)
    {
        this.Name = bookName;
        this.Date = dateAsDate;
    }
}

class Program
{
    static void Main(string[] args)
    {
        var nameAndDate = new Dictionary<string, Book> {
            {"HP1", new Book("HP1", new DateTime(1997,6,26)) },
            {"HP7", new Book("HP1", new DateTime(2007,7,21)) },
            {"AC", new Book("AC", new DateTime(2009,11,20)) },
        };

        foreach (var book in nameAndDate.OrderBy(b => b.Value.Date).ThenBy(b => b.Value.Name))
        {
            Console.WriteLine($"{book.Key} -> {book.Value.Date.ToString("dd.MM.yyyy")}");
        }
        Console.ReadKey();
    }
}
}
asidis
  • 1,374
  • 14
  • 24
  • Well sorting this way is easy , I forgot to include my input. It is pretty much this : AC OBowden PenguinBooks 20.11.2009 0395082555 14.00 | HP1 JKRowling Bloomsbury 26.06.1997 0395082777 15.50 | HP7 JKRowling Bloomsbury 21.07.2007 0395082666 20.00 | – Marto99 Jun 14 '18 at 08:50
  • @asidis "by name _and_ date" - you didn't include the date part. – bommelding Jun 14 '18 at 08:51
  • I thought it was obvious – Marto99 Jun 14 '18 at 08:53
  • @Marto99 I adapted the code based on your comment. Didn't get your ordering rule at first. – asidis Jun 14 '18 at 08:55