C# - sorting by a property

后端 未结 3 1227
梦谈多话
梦谈多话 2021-01-27 19:22

I am trying to sort a collection of objects in C# by a custom property. (For context, I am working with the Twitter API using the Twitterizer library, sorting Direct Messages in

相关标签:
3条回答
  • 2021-01-27 20:01

    You say sorting but it sounds like you're trying to divide up a list of things based on a common value. For that you want GroupBy.

    You'll also want ToDictionary to switch from an IGrouping as you'll presumably be wanting key based lookup.

    I assume that the elements within each of the output sets will need to be sorted, so check out OrderBy. Since you'll undoubtedly be accessing each list multiple times you'll want to collapse it to a list or an array (you mentioned list) so I used ToList

    //Make some test data
    var labels = new[] {"A", "B", "C", "D"};
    var rawMessages = new List<Message>();
    
    for (var i = 0; i < 15; ++i)
    {
        rawMessages.Add(new Message
        {
            Label = labels[i % labels.Length],
            Text = "Hi" + i,
            Timestamp = DateTime.Now.AddMinutes(i * Math.Pow(-1, i))
        });
    }
    
    //Group the data up by label
    var groupedMessages = rawMessages.GroupBy(message => message.Label);
    
    //Convert to a dictionary for by-label lookup (this gives us a Dictionary<string, List<Message>>)
    var messageLookup = groupedMessages.ToDictionary(
                //Make the dictionary key the label of the conversation (set of messages)
                grouping => grouping.Key, 
                //Sort the messages in each conversation by their timestamps and convert to a list
                messages => messages.OrderBy(message => message.Timestamp).ToList());
    
    //Use the data...
    var messagesInConversationA = messageLookup["A"];
    var messagesInConversationB = messageLookup["B"];
    var messagesInConversationC = messageLookup["C"];
    var messagesInConversationD = messageLookup["D"];
    
    0 讨论(0)
  • 2021-01-27 20:03

    It sounds to me like mlorbetske was correct in his interpretation of your question. It sounds like you want to do grouping rather than sorting. I just went at the answer a bit differently

    var originalList = new[] { new { Name = "Andy", Label = "Junk" }, new { Name = "Frank", Label = "Junk" }, new { Name = "Lisa", Label = "Trash" } }.ToList();
    
    var myLists = new Dictionary<string, List<Object>>();
    
    originalList.ForEach(x =>
        {
            if (!myLists.ContainsKey(x.Label))                
                myLists.Add(x.Label,new List<object>());
            myLists[x.Label].Add(x);
    
        });
    
    0 讨论(0)
  • 2021-01-27 20:22

    Have you tried Linq's OrderBy?

    var mySortedList = myCollection.OrderBy(x => x.PropertyName).ToList();
    

    This is still going to loop through the values to sort - there's no way around that. This will at least clean up your code.

    0 讨论(0)
提交回复
热议问题