Most of the time, the elements returned using a specific search/filtering criteria are usable as it. In some cases, it might be required to display, say, a list of views from the current Revit model in your Revit plug-in, alphabetically. Since we can use Linq queries to generate the list of views in a given model, we can use ‘OrderBy’ in our Linq query to get the result we need. The following short code snippet shows the appropriate use of the ‘OrderBy’ in the query to list the view names alphabetically.
using System;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
namespace Revit.SDK.Samples.HelloRevit.CS
{
[Transaction(TransactionMode.Manual)]
public class Command : IExternalCommand
{
public Result Execute(ExternalCommandData commandData,
ref string message,
ElementSet elements)
{
UIApplication app = commandData.Application;
Document doc = app.ActiveUIDocument.Document;
FilteredElementCollector coll =
new FilteredElementCollector(doc).OfClass(typeof(ViewPlan));
IOrderedEnumerable<ViewPlan> vps =
from ViewPlan vp in coll orderby vp.ViewName ascending select vp;
foreach (ViewPlan v in vps)
{
TaskDialog.Show("Names", v.ViewName);
}
return Result.Succeeded;
}
}
}
The above mentioned approach can be used for sorting any list of elements filtered from a Revit model.