寻找更好的方式来整理我的名单,其中,T> [英] Looking for a better way to sort my List<T>

查看:209
本文介绍了寻找更好的方式来整理我的名单,其中,T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我检讨了一块code我写的不是很久以前,我只是恨我处理的排序方式 - 我想知道如果任何人可以给我一个更好的办法

I'm reviewing a piece of code I wrote not too long ago, and I just hate the way I handled the sorting - I'm wondering if anyone might be able to show me a better way.

我有一个类,控股,其中包含了一些信息。我有另一个类, HoldingsList ,其中包含一个名单,其中,控股> 成员。我也有一个枚举, PortfolioSheetMapping ,其中有〜40左右的元素。

I have a class, Holding, which contains some information. I have another class, HoldingsList, which contains a List<Holding> member. I also have an enum, PortfolioSheetMapping, which has ~40 or so elements.

之类的它看起来是这样的:

It sort of looks like this:

public class Holding
{
    public ProductInfo Product {get;set;} 
    // ... various properties & methods ...
}

public class ProductInfo
{
    // .. various properties, methods... 
}

public class HoldingsList
{
    public List<Holding> Holdings {get;set;}
    // ... more code ...
}

public enum PortfolioSheetMapping
{
    Unmapped = 0,
    Symbol,
    Quantitiy,
    Price,
    // ... more elements ...
}

我有一个可以调用列表,这取决于枚举用户选择进行排序的方法。该方法使用已超过40例盟switch语句(啊!)。

I have a method which can invoke the List to be sorted depending on which enumeration the user selects. The method uses a mondo switch statement that has over 40 cases (ugh!).

一个短的片段下方显示了code:

A short snippet below illustrates the code:

if (frm.SelectedSortColumn.IsBaseColumn)
{
    switch (frm.SelectedSortColumn.BaseColumn)
    {
        case PortfolioSheetMapping.IssueId:
            if (frm.SortAscending)
            {
                // here I'm sorting the Holding instance's
                // Product.IssueId property values...
                // this is the pattern I'm using in the switch...
                pf.Holdings = pf.Holdings.OrderBy
                  (c => c.Product.IssueId).ToList();
            }
            else
            {
                pf.Holdings = pf.Holdings.OrderByDescending
                  (c => c.Product.IssueId).ToList();
            }
            break;
        case PortfolioSheetMapping.MarketId:
            if (frm.SortAscending)
            {
                pf.Holdings = pf.Holdings.OrderBy
                  (c => c.Product.MarketId).ToList();
            }
            else
            {
                pf.Holdings = pf.Holdings.OrderByDescending
                  (c => c.Product.MarketId).ToList();
            }
            break;
        case PortfolioSheetMapping.Symbol:
            if (frm.SortAscending)
            {
                pf.Holdings = pf.Holdings.OrderBy
                  (c => c.Symbol).ToList();
            }
            else
            {
                pf.Holdings = pf.Holdings.OrderByDescending
                  (c => c.Symbol).ToList();
            }
            break;
        // ... more code ....

我的问题是与switch语句。该开关紧密结合的 PortfolioSheetMapping 枚举,其中明天或隔天可以改变。每次它改变的时候,我将不得不重新审视这个switch语句,并添加另一个情况阻止它。我只是担心,最终这个switch语句会增长如此之大,这是完全无法控制的。

My problem is with the switch statement. The switch is tightly bound to the PortfolioSheetMapping enum, which can change tomorrow or the next day. Each time it changes, I'm going to have to revisit this switch statement, and add yet another case block to it. I'm just afraid that eventually this switch statement will grow so big that it is utterly unmanageable.

谁能告诉我,如果有更好的方法来进行排序我的名单?

Can someone tell me if there's a better way to sort my list?

推荐答案

您正在重新分配排序的数据直接回到你的 pf.Holdings 属性,所以为什么不能绕过排序依据的开销和了ToList ,只是使用了列表的的 排序 的方法,而不是直接?

You're re-assigning the sorted data straight back to your pf.Holdings property, so why not bypass the overhead of OrderBy and ToList and just use the list's Sort method directly instead?

您可以使用地图来保存 比较&LT; T&GT ; 代表所有支持的分类法,然后调用的 排序(比较&LT; T&GT;) 用适当的委托:

You could use a map to hold Comparison<T> delegates for all the supported sortings and then call Sort(Comparison<T>) with the appropriate delegate:

if (frm.SelectedSortColumn.IsBaseColumn)
{
    Comparison<Holding> comparison;
    if (!_map.TryGetValue(frm.SelectedSortColumn.BaseColumn, out comparison))
        throw new InvalidOperationException("Can't sort on BaseColumn");

    if (frm.SortAscending)
        pf.Holdings.Sort(comparison);
    else
        pf.Holdings.Sort((x, y) => comparison(y, x));
}

// ...

private static readonly Dictionary<PortfolioSheetMapping, Comparison<Holding>>
    _map = new Dictionary<PortfolioSheetMapping, Comparison<Holding>>
    {
        { PortfolioSheetMapping.IssueId,  GetComp(x => x.Product.IssueId) },
        { PortfolioSheetMapping.MarketId, GetComp(x => x.Product.MarketId) },
        { PortfolioSheetMapping.Symbol,   GetComp(x => x.Symbol) },
        // ...
    };

private static Comparison<Holding> GetComp<T>(Func<Holding, T> selector)
{
    return (x, y) => Comparer<T>.Default.Compare(selector(x), selector(y));
}

这篇关于寻找更好的方式来整理我的名单,其中,T&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆