Sort List elements by value parameter

Sort List elements by value parameter

jamess166
Advocate Advocate
15,024 Views
3 Replies
Message 1 of 4

Sort List elements by value parameter

jamess166
Advocate
Advocate
Good day, I'm learning c #, I have a list obtained through a pickobjects, which I want to sort according to the value of a parameter. something similar to what the python node of the image does.
0 Likes
Accepted solutions (1)
15,025 Views
3 Replies
Replies (3)
Message 2 of 4

Anonymous
Not applicable
Accepted solution

If you haven't learned about labda/predicates and Linq yet, those are great concepts to learn in C#. Then you can use the OrderBy method to sort.

 

However, in your case, you'd have to combine it with some more code:

PickObjects(), returns an IList<Reference>

From each Reference you need to get the Element, then you have to get the value of the parameter you need, then sort by that value. Something like this (very simplified):

 

// Get picked object references
IList<Reference> pickedObjects = uidoc.Selection.PickObject(...); // Convert all references to elements List<Element> pickedElements = pickedObjects.Select(x=>doc.GetElement(x)).ToList(); // Sort all elements by the value of "MY_PARAMETER" List<Element> sortedElements = pickedElements.OrderBy(x=>x["MY_PARAMETER"].AsDouble()).ToList();

Be aware, that this is not very efficient as you will have to query "MY_PARAMETER" more than once for each object (the OrderBy function will do that). So if it's too slow you will have to cache the parameter value in something like a Tuple<Double, Element>.

 

Also, you should handle errors when the parameter isn't present on an object or parameter doesn't have a value.

Message 3 of 4

jamess166
Advocate
Advocate

Thanks, apparently I have to resort to the lambda code which I do not know, any website or blog where I can learn it. Thank you.

0 Likes
Message 4 of 4

Anonymous
Not applicable

You can think of a lambda as just a function without a name (syntactic sugar done by the compiler) can be as simple as:

 

Imagine you have:

public class Thing
{
    public int Index { getset; }
    public string Name { getset; }
 
    public Thing(int index, string name)
    {
        Index = index;
        Name = name;
    }
}

public static string GetThingName(Thing x) {     return x.Name; }

public static int GetThingIndex(Thing x)
{
return x.Index;
}

 

Then, you could order an array of Things like this:

 

Thing[] things = new Thing[] { new Thing(1"one"), new Thing(11"eleven"), new Thing(4"four") };
// Order by the index, the following two lines do the exact same thing:
things.OrderBy(x => x.Index); // With a labda things.OrderBy(GetThingIndex); // With a dedicated function
// Order by the name, the following two lines do the exact same thing:
things.OrderBy(x => x.Name); // With a lambda
things.OrderBy(GetThingName); // With a dedicated function

 

So basically, lambdas allow you to write less code (and, of course, they can be much more complicated than example above). Hope this helps. You can read more on these sites:

https://linqsamples.com/linq-to-objects/ordering/OrderBy-numbers-lambda

https://weblogs.asp.net/dixin/understanding-csharp-features-5-lambda-expression