[Solved] iComparable error when using OrderBy
# help-with-umbraco
c
Umb V13.4.0 I'm adding a change to a working page, adding a list order to a list of categories so they can be listed in the required order. In a BlockGrid Component View I get a list that could be one of two things, an article list or a category list. I get the appropriate list of children with
var listItems = Model.Content.ItemsToList.Children.Take(displayNum);
If it's a category list then it will have a "listOrderIndexNumber" field. As I want to output to the page in the correct order, after checking listItems is CategoryList I then try to output the CategoryPages in order like so:-
Copy code
@foreach (CategoryPage item in listItems.OrderBy(x => x.GetProperty("listOrderIndexNumber"))) {
........
}
However during the processing of the foreach I get "ArgumentException: At least one object must implement IComparable." I've tried various things like taking off the Take(displayNum), rerunning the collection with a different var after checking for CategoryList, but I get the same error all the time. I thought this would be simple, just order it by value, but there's something stopping me and I can't work out what it is. Googling hadn't helped. There's a separate foreach for articleLists. I'm using one view for two, very similar purposes. If anyone has any advice I'd love to hear it. Thanks.
j
x.GetProperty returns an
IPublishedProperty?
as far as I can see. Which it doesn't know how to compare several items of to order them. You probably want to just get the value of the property cast to an int (I assume its a number?) If I understand your code right then it should work with this:
Copy code
csharp

foreach (CategoryPage item in listItems.OrderBy(x => x.Value<int>("listOrderIndexNumber"))) 
{
  ........
}
c
Perfect. Thanks so much. 🙂
2 Views