0

I have the list like this

public class Col
{
    public long Id { get; set; }
    public Dictionary<string, dynamic> Dt { get; set; }
}

var list = IEnumerable<Col>;

I need to sort the List using dictionary

I tried a lot of methods, but nothing helps, there is an error:

System.ArgumentException: At least one object must implement IComparable.

The last thing I stopped at was:

list.OrderBy(x => x.Dt.Where(r => r.Key == "Smile").Select(r => r.Value));

Sorting should be done by the Dictionary value

Update1:

I want to sort the block that I have selected, but sort not by Id, but by the data in the dictionary enter image description here

Betsq9
  • 135
  • 1
  • 3
  • 11
  • 1
    Dictionaries have multiple values, before thinking about the implementation, you'll need to define how the result should look like. You should add some example data to clarify this. – Christoph Lütjen Sep 03 '21 at 19:23
  • 1
    https://stackoverflow.com/questions/5333571/how-to-sort-a-list-of-objects-by-dictionary-value – Henrique Mauri Sep 03 '21 at 19:25
  • I have updated my question – Betsq9 Sep 03 '21 at 19:35
  • It's still not clear what "order by the data" means. Do you want to sort by product name (key 646) or article number (key 647) or variant key (key 650) or price or do you want to pass in the key to be used for sorting? Will the keys be fixed (=product name has always key 646)? Perhaps it would simplify things if you create a class for your products? – Christoph Lütjen Sep 03 '21 at 19:48
  • For example, a request for key "645" comes and I need to sort `list = IEnumerable ;` which has a dictionary. All dictionaries have a key "645", I need to find a column by key 645 and sort by the value of this column – Betsq9 Sep 03 '21 at 19:56
  • Sort the list by dictionary values. Dictionary we don't do anything at all – Betsq9 Sep 03 '21 at 19:57
  • 1
    Sounds like `list.OrderBy(c => c.Dt["645"]);`? – Christoph Lütjen Sep 03 '21 at 20:17
  • Does this answer your question? [get dictionary value by key](https://stackoverflow.com/questions/12169443/get-dictionary-value-by-key) – Christoph Lütjen Sep 03 '21 at 20:19
  • @ChristophLütjen that work `list.OrderBy(c => c.Dt["645"]);`. Write your answer I will mark as correct – Betsq9 Sep 03 '21 at 20:35

1 Answers1

2

Try this:

 list.OrderBy(c => c.Dt[<YourKeyValue>]);
Misha Zaslavsky
  • 8,414
  • 11
  • 70
  • 116
  • 1
    I don't think that's what OP wants. If I understand correctly, they want to sort by `value` in the dictionary. And since the value is defined as `dynamic`, system throws the error – Felix Sep 03 '21 at 19:25