I already saw this question, but i didn"t discover my delight with the answers...

You are watching: At least one object must implement icomparable

I"m trying to carry out that:

var coll = JsonConvert.DeserializeObject>(json);coll = coll.OrderBy(a => a.tags).Distinct().ToList();Throws an error:

At the very least one object should implement IComparable.

For the moment i didn"t find the equipment so ns did that:

List category = brand-new List(); var coll = JsonConvert.DeserializeObject>(json);for (int i = 0; i It works yet i"m curious to recognize why the very first one don"t work.

EDIT :

My data come native a JSON :

"tags": < "Pantoufle", "Patate" > }, public list tags get; set;
c# linq json.net
share
enhance this question
follow
edited Apr 6 "15 in ~ 4:04

*

Iswanto mountain
17.4k1212 yellow badges5656 silver badges7979 bronze badges
inquiry Apr 6 "15 in ~ 3:51
*

Fly_federerFly_federer
20611 gold badge22 silver- badges1111 bronze title
4
add a comment |

2 answers 2


energetic oldest Votes
26
To stimulate a collection of things, there must be a means to to compare two things to determine which one is larger, or smaller sized or even if it is they room equal. Any c# form that implements the IComparable interface, provides the method to to compare it versus one more instance.

Your tags ar is a list of strings. There is no standard means to compare 2 lists the strings in the manner. The kind List does no implement the IComparable interface, and also thus cannot be supplied in a LINQ OrderBy expression.

If for example you wanted to stimulate the posts by the variety of tags, you might do that prefer this:

coll = coll.OrderBy(a => a.tags.Count).ToList();because Count will return an integer and also an creature is comparable.

See more: Which Of The Following Is True About Cloud Computing, Chapter 6 Review Mis Flashcards

If you wanted to acquire all distinct tags in sorted order, you could do that prefer this:

var sortedUniqueTags = coll .SelectMany(a => a.Tags) .OrderBy(t => t) .Distinct() .ToList();because a string is comparable.

If you yes, really know exactly how to compare 2 lists the strings, you might write your own custom comparer:

public class MyStringListComparer : IComparer> // implementationand use it choose this:

var comparer = brand-new MyStringListComparer();coll = coll.OrderBy(a => a.tags, comparer).Distinct().ToList();