3

I've written a function to sort a ListView when a column header is clicked. I now want the function to work for any column. My first thought was to pass a string representing the property being displayed in the column and using if or switch to sort by the correct property E.g.:

private bool sortOrder = false;
public void SortCol(string property)
{
    OUModel ou = ADRoot.Descendants().Where(node => node.IsSelected == true).FirstOrDefault() as OUModel;

    if (ou != null)
    {
        switch (property)
        {
            case "Name":
                if (sortOrder)
                {
                    ou.Computers = new List<RemoteComputer>(ou.Computers.OrderBy(c => c.Name));
                }
                else
                {
                    ou.Computers = new List<RemoteComputer>(ou.Computers.OrderByDescending(c => c.Name));
                }
                break;

            case "LastUpdated":
                if (sortOrder)
                {
                    ou.Computers = new List<RemoteComputer>(ou.Computers.OrderBy(c => c.LastUpdated));
                }
                else
                {
                    ou.Computers = new List<RemoteComputer>(ou.Computers.OrderByDescending(c => c.LastUpdated));
                }
                break;
                // etc...
        }
        sortOrder = !sortOrder;
    }
}

But this will obviously get quite long and repetitive. Instead I wondered if I could pass the property itself. But I'm trying to pass the property type from XAML (using Caliburn.Micro Actions) and can't figure out how.

private bool sortOrder = false;
public void SortCol<T>(Func<RemoteComputer, T> property)
{
    OUModel ou = ADRoot.Descendants().Where(node => node.IsSelected == true).FirstOrDefault() as OUModel;

    if (ou != null)
    {
        if (sortOrder)
        {
            ou.Computers = new List<RemoteComputer>(ou.Computers.OrderBy(property));
        }
        else
        {
            ou.Computers = new List<RemoteComputer>(ou.Computers.OrderByDescending(property));
        }
        sortOrder = !sortOrder;
    }
}

XAML:

    <!-- Name Spaces
    xmlns:models="clr-namespace:MyProject.Models"
    xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
    xmlns:cal="http://www.caliburnproject.org"
    -->

    <!-- TreeView the ListView is bound to -->
    <TreeView x:Name="ADTree" ItemsSource="{Binding ADRoot.Children}">
    <!-- -->

    <ListView x:Name="ADComputers" ItemsSource="{Binding SelectedItem.Computers, ElementName=ADTree, Mode=TwoWay}" SelectionMode="Multiple" ItemContainerStyle="{DynamicResource RemoteComputerItem}">
        <ListView.View>
            <GridView>
                <GridViewColumn x:Name="Name" Width="120" DisplayMemberBinding="{Binding Name}">
                    <GridViewColumn.Header>
                        <GridViewColumnHeader x:Name="SortName" Content="Computer Name">
                            <i:Interaction.Triggers>
                                <i:EventTrigger EventName="Click">
                                    <cal:ActionMessage MethodName="SortCol">
     <!-- Trying to pass type here, but nested types are not supported. Don't know how else I can do this -->
                                        <cal:Parameter Value="{x:Type models:RemoteComputer.Name}" />
                                    </cal:ActionMessage>
                                </i:EventTrigger>
                            </i:Interaction.Triggers>
                        </GridViewColumnHeader>
                    </GridViewColumn.Header>
                </GridViewColumn>
                <!-- Other Columns -->
            </GridView>
        </ListView.View>
    </ListView>

Classes etc:

class ADTreeViewModel : OUModel
{    
    public OUModel ADRoot { get; set; } = new OUModel(null, false); // <<< TreeView is bound to this, and ListView is bound to TreeView.SelectedItem.Computers
}

class OUModel : PropertyChangedBase, ITreeViewItemViewModel
{
    private List<RemoteComputer> computers = new List<RemoteComputer>();
    public List<RemoteComputer> Computers { get { return computers; } set { computers = value; NotifyOfPropertyChange(); } }
}

class RemoteComputer : PropertyChangedBase
{
    public string Name
    {
        get { return name; }
        set { name = value; NotifyOfPropertyChange(() => Name); }
    }

    // Shortening the other properties, but you get the idea...
    public IPAddress IP { //etc }
    public ComputerStatus Status { //etc }
    public DateTime LastUpdated { //etc }

    // and so on...
}

1 Answer 1

1

you don't need a switch. if you wanna access your properties by name, you can create an Expression to build a Func

private bool sortOrder = false;
public void SortCol(string propName)
{
    OUModel ou = ADRoot.Descendants().Where(node => node.IsSelected == true).FirstOrDefault() as OUModel;

    if (ou != null)
    {
        if (sortOrder)
            ou.Computers = ou.Computers.OrderBy(GetProperty(propName)).ToList();
        else
            ou.Computers = ou.Computers.OrderByDescending(GetProperty(propName)).ToList();
        sortOrder = !sortOrder;
    }
}

// this method creates an Expression<Func<RemoteComputer,object>>
// and returning its compiled() resutl. Func<RemoteComputer,object>
// so you can use this `Func` in your orderBy methods.
public Func<T, object> GetProperty<T>(string propertyName)
{
// q =>
    ParameterExpression param = Expression.Parameter(typeof(T), "q");
// q.propertyName  
// for example : q.Name
    MemberExpression member = Expression.PropertyOrField(param, propertyName);
// change property type to object (because we want to use all posible types of our class peroperties or fields)
    var body = Expression.Convert(member, typeof(object));
// create Expression<Func<RemoteComputer,object>>
    var property = Expression.Lambda<Func<T, object>>(body, param);
// returns a Func<T,object>
    return property.Compile();
}


Update

also, you can use this extension class to add OrderBy string support to your lists in the entire application. this also works for IQueryable queries.

public static class OrderByExtentions
{
    public static IOrderedEnumerable<T> OrderByDescending<T>(this IEnumerable<T> list, string propertyName )
    {
        return list.OrderByDescending(GetPropertyFunc<T>(propertyName));
    }
    public static IQueryable<T> OrderByDescending<T>(this IQueryable<T> list, string propertyName)
    {
        return list.OrderByDescending(GetPropertyExpression<T>(propertyName));
    }
    public static IOrderedEnumerable<T> OrderBy<T>(this IEnumerable<T> list, string propertyName)
    {
        return list.OrderBy(GetPropertyFunc<T>(propertyName));
    }
    public static IQueryable<T> OrderBy<T>(this IQueryable<T> list, string propertyName)
    {
        return list.OrderBy(GetPropertyExpression<T>(propertyName));
    }
    private static Expression<Func<T, object>> GetPropertyExpression<T>(string propertyName)
    {
        ParameterExpression param = Expression.Parameter(typeof(T), "q");
        MemberExpression member = Expression.PropertyOrField(param, propertyName);
        var body = Expression.Convert(member, typeof(object));
        return Expression.Lambda<Func<T, object>>(body, param);
    }
    private static Func<T, object> GetPropertyFunc<T>(string propertyName)
    {
        return GetPropertyExpression<T>(propertyName).Compile();
    }
}

usage:

// eg. propName = "Name";

ou.Computers = ou.Computers.OrderByDescending(propName).ToList();

// or

ou.Computers = ou.Computers.OrderBy(propName).ToList();
3
  • That works great, thanks. Would you mind explaining the GetProperty method a little please?
    – Daniel
    Commented Feb 9, 2020 at 13:16
  • @Daniel I added some comments on each step. hope it help Commented Feb 9, 2020 at 14:31
  • @Daniel if you don't mind please change your question title to something like OrderBy list by property name this way you can help other people have a similar problems. Commented Feb 9, 2020 at 14:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.