Owain Reed
Owain Reed

Reputation: 333

Sort By Int Property of Node List - Umbraco Razor

I am trying to sort a DynamicNodeList by a numeric property "cost". I've tried loads of different ways here but am completely at a loss:

var nodes = Model.AncestorOrSelf(1).Descendants().Where("NodeTypeAlias.Equals(\"Event\")").Where("Visible");

nodes = nodes.OrderBy("Int32.Parse(cost.Value)");

Gives me the error: No property or field 'Value' exists in type 'Func`2'

I've got a feeling this is as close as I've gotten! Does anybody have any clues on this?

EDIT:

OK I solved this by changing the NodeList:

var nodes = Model.AncestorOrSelf(1).Descendants().Where("NodeTypeAlias.Equals(\"Event\")").Where("Visible").Where("cost > 0");

Upvotes: 1

Views: 3266

Answers (3)

BeaverProj
BeaverProj

Reputation: 2215

I have had issues when sorting on something more than a single item or something that isn't simple. In those cases, I convert it to a List or DynamicNode objects and use the Sort method with a delegate.

Something like this:

possibleListings.Sort(delegate(DynamicNode x, DynamicNode y)
{       
  ... perform sorting logic
});

Then you can do Int32.TryParse() calls or whatever you need inside the delegate.

Upvotes: 0

Douglas Ludlow
Douglas Ludlow

Reputation: 10942

nodes = nodes.OrderBy("cost");

...should work just fine.

Refer to Umbraco Razor Feature Walkthrough – Part 4 for more info on OrderBy().

Upvotes: 2

The_Cthulhu_Kid
The_Cthulhu_Kid

Reputation: 1859

You could try

Dictionary<DynamicNode, int> myVar = new Dictionary<DynamicNode, int>();
foreach(var node in nodes)
{
    myVar.Add(node,node.GetPropertyValue("cost");
}

and then sort that. That's the only way I can think of.

Upvotes: 0

Related Questions