1

I want to restructure my data from this question: Sorting elements inside a tuple by inner elements so that I can use LINQ to sort with the OrderBy operation. But the dictionary I have is transposed the wrong way.

  public class Gamedat
    {
        public string[] Games { get; set; }

        public string[] Gtypes { get; set; }

        public Dictionary<string, int[]> Playdata { get; set; }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Gamedat dataframe = new Gamedat();
            dataframe.Games = new string[] { "Game 1", "Game 2", "Game 3", "Game 4", "Game 5" };
            dataframe.Gtypes = new string[] { "A", "B", "C", "B", "A" };
            dataframe.Playdata = new Dictionary<string, int[]>();

            //Add dictionary items
            int[] a = { 1, 0, 3, 4, 0 };
            int[] b = { 3, 0, 9, 10, 0 };
            int[] c = { 2, 3, 3, 5, 0 };

            dataframe.Playdata.Add("Jack", a);
            dataframe.Playdata.Add("Jane", b);
            dataframe.Playdata.Add("James", c);
        }

What is a good way to structure the data for sorting without losing the keys from the dictionary? One possibility I thought of for sorting without the dictionary is:

  public class Gamedat
    {
        public string Games { get; set; }

        public string Gtypes { get; set; }   
    }

    class Program
    {
        static void Main(string[] args)
        {
        List<Gamedat> dataframe = new List<Gamedat> {new Gamedat { Game = "Game 1", Gtypes = "A"},
        new Gamedat { Game = "Game 2", Gtypes = "B"},
        new Gamedat { Game = "Game 3", Gtypes = "C"},
        new Gamedat { Game = "Game 4", Gtypes = "B"},
        new Gamedat { Game = "Game 5", Gtypes = "A"},
        };


            var result = dataframe.OrderBy(x => x.Gtypes);
        }
Community
  • 1
  • 1
ccsv
  • 8,188
  • 12
  • 53
  • 97
  • Try this sorting method.. http://stackoverflow.com/questions/22341378/sort-a-list-by-column-name-string – User2012384 Jul 31 '15 at 03:04
  • @User2012384 That would work except for the dictionary being transposed the other way – ccsv Jul 31 '15 at 03:07
  • What are you trying to sort, exactly? `Jack Jane James`, or do you want to sort the collection of ints for each entry? Then your attempt sorts `Gtypes` which isn't related to the dictionary at all. – Rob Jul 31 '15 at 03:15
  • @Rob I want to sort the ints in the dictionary which has the index as `Game` – ccsv Jul 31 '15 at 03:28
  • @ccsv You most definitely can sort them then, either at insert: `dataframe.Playdata.Add("Jack", a.OrderBy(b => b).ToArray());` or at retrieval: `dataframe.Playdata["Jack"].OrderBy(a => a);` – Rob Jul 31 '15 at 03:31
  • @Rob The way you sorted them would be out of order since the index is not according to `Game` and it is not being sorted by `Gtypes` – ccsv Jul 31 '15 at 03:38
  • 1
    @ccsv Then your lists are explicitly linked? (I.e. they should always be in the same order as each other?). Then you shouldn't really be storing them in different collections. You should have *one* class which stores the information, and have a list of objects of that class. – Rob Jul 31 '15 at 03:41
  • If you want to keep denormalized data about game - this is duplicate of http://stackoverflow.com/questions/3945935/sort-one-c-sharp-list-by-another – Alexei Levenkov Jul 31 '15 at 03:45
  • @Rob Lets say I switched their order as you said using the transpose function in LINQ in http://stackoverflow.com/questions/6950495/linq-swap-columns-into-rows . The data would match as a set of 5 and I can use `List`, but I will also lose information on the dictionary keys. – ccsv Jul 31 '15 at 03:54

1 Answers1

3

You are missing the OOP Concept of Composition.

If you composite that key and Value to one object type you don't have to transpose it at all. You can use just a List instead of a Dictionary. The fact that you have to transpose gives a bad design smell.

GameType should be a property of the Game. Then you don't need two list. Just one List<Game> would be enough and then you have no problem of sorting the Games list by GameType.

CharithJ
  • 46,289
  • 20
  • 116
  • 131
  • that is not the main problem as it is solved here: http://stackoverflow.com/questions/2277189/sorting-a-list-of-class-with-linq. The main problem is the transpose of the `Dictionary` – ccsv Jul 31 '15 at 04:02
  • @ccsv: If you composit that key and Value to one object type you don't have to transpose it at all. You can use just a List instead of a Dictionary. The fact that you have to transpose gives a bad design smell. – CharithJ Jul 31 '15 at 04:09
  • I cannot use a list or I will lose the keys from the dictionary – ccsv Jul 31 '15 at 05:03
  • 1
    @cssv In any case, sorting the `values` on the dictionary do not affect the keys at all. Secondly, you can use a `List>` instead of a `Dictionary`. – Rob Jul 31 '15 at 05:12
  • @ccsv: If you composite everything (to one object) that you are scare to lose, there is nothing to lose. – CharithJ Jul 31 '15 at 05:25