views:

9

answers:

1

Hi guys,

I've just started to learn WPF/C# and I picked a project which would actually be useful to me instead of variations of Hello, World! programs.

It's a small app that polls a game server for player information and binds it to a Datagrid. The data is grouped by team, which can be one of four values: Blue, Red, Spectator, or None.

I've got the Linq query working fine, and the Datagrid grouping is almost good, except for one small problem: the order of the four teams groups is different every time. Sometimes Red is first, sometimes None, etc.

Is there any way I can force the groups into the order above?

Here is the Linq query (addr is the server ip):

private void get_server(string addr)
    {
        var loc = "http://ukcs.gameme.com/api/serverinfo/" + addr + "//players";
        XDocument doc = XDocument.Load(@loc);

        var server = new ObservableCollection<Player>
            (from player in doc.Descendants("player")
            orderby player.Element("rank").Value
            select new Player
            {
                name = player.Element("name").Value,
                team = player.Element("team").Value,
                frags = player.Element("kills").Value + ":" + player.Element("deaths").Value,
                rank = int.Parse(player.Element("rank").Value)
            });

        server.OrderBy(p => p.rank);
        ListCollectionView collection = new ListCollectionView(server);
        collection.GroupDescriptions.Add(new PropertyGroupDescription("team"));
        player_list.ItemsSource = collection;
    }

A second problem is that neither of the OrderBys seem to have an effect.

Any help would be appreciated!

A: 

To answer your last question first :) The OrderBy's have no meaning here, because after you sort the players, you put the list inside a CollectionView with a grouping which promptly sorts it by Team (since the grouping is on Team).

To get them in the proper order, you can sort them by Team before putting them inside the ListCollectionView. However, this will put them in alphabetical order - and not exactly the order you wanted. You need to implement IComparable and put that in your sort method - i've rewritten your method a bit (I am not so good at that Linq-form - so, bear with me :)):

You made it a bit harder on yourself by introducing a few unnecessary things - I've tried to root them out.

private void get_server(string addr)
{
   var loc = "http://ukcs.gameme.com/api/serverinfo/" + addr + "//players";
   var doc = XDocument.Load(@loc);

   var server = doc.Descendants("player")
            .Select(player => new Player
                                  {
                                      name =player.Element("name").Value,
                                      team=player.Element("team").Value,
                                      frags=player.Element("kills").Value +":" +player.Element("deaths").Value,
                                      rank=int.Parse(player.Element("rank").Value)
                                  })
            .OrderBy(p => p.team,new CustomSort())
            .ThenBy(p => p.rank).ToList();
    var collection = new ListCollectionView(server);
    collection.GroupDescriptions.Add(new PropertyGroupDescription("team"));
    player_list.ItemsSource = collection;
}
public class CustomSort : IComparer<string>
{
    public int Compare(string x, string y)
    {
        if (x.Equals(y))
            return 0;
        if (y.Equals("None") || x.Equals("Blue"))
            return 1;
        if (x.Equals("None") || y.Equals("Blue"))
            return -1;
        if (x.Equals("Red")|| y.Equals("Spectator"))
            return -1;
        return 1; // y == "Red" and x == "Spectator"
    }
}

Hope this helps!

Goblin
Thanks a lot! I knew there must be a way but WPF is still strange to me.Everything works perfectly and you've saved me days of staring at the monitor scratching my head. Thanks again!
coldandtired