5

我刚刚编写了以下代码,它将按其本机排序字符串,string.Compare()但允许一组异常(在本例customPriority中)将优先于默认string.Compare()函数。

这一切似乎有点啰嗦,我想知道 .NET 中是否有内置的东西允许这样做?

    var unorderered = new[] { "a", "b", "c", "x", "y", "z" };
    var ordered = unorderered.OrderBy(a => a, new CustomStringComparer());
    //expected order y,x,a,b,c,z

class CustomStringComparer : IComparer<string>
{
    int IComparer<string>.Compare(string x, string y)
    {
        if (x == y)
            return 0;
        else
        {
            //----------------------------
            //beginning of custom ordering
            var customPriority = new[] { "y", "x" };
            if (customPriority.Any(a => a == x) && customPriority.Any(a => a == y)) //both in custom ordered array
            {
                if (Array.IndexOf(customPriority, x) < Array.IndexOf(customPriority, y))
                    return -1;                   
                return 1;
            }
            else if (customPriority.Any(a => a == x)) //only one item in custom ordered array (and its x)                    
                return -1;
            else if (customPriority.Any(a => a == y)) //only one item in custom ordered array (and its y)                    
                return 1;
            //---------------------------
            //degrade to default ordering
            else
                return string.Compare(x, y);

        }
    }
}
4

3 回答 3

4

首先,我认为重述问题很有用:您想按以下方式排序:

  1. 给定数组中的索引;如果该项目不在数组中,则索引为无穷大
  2. 字符串本身

OrderBy()这意味着您可以通过使用第一个条件,然后使用第二个条件来实现排序顺序ThenBy()

private static uint NegativeToMaxValue(int i)
{
    if (i < 0)
        return uint.MaxValue;
    return (uint)i;
}

…

var ordered = unorderered
    .OrderBy(a => NegativeToMaxValue(Array.IndexOf(new[] { "y", "x" }, a)))
    .ThenBy(a => a);

NegativeToMaxValue()是必要的,因为不在数组中的项目应该在最后,但通常它们会在第一个,因为索引是-1。(一种骇人听闻且难以理解的方法是直接将结果转换为IndexOf()to uint。)

如果您想通过创建一个 .Net 来重用这种排序IComparer,我相信 .Net 中没有任何东西可以帮助您。但是您可以改用ComparerExtensions

IComparer<string> comparer = KeyComparer<string>
    .OrderBy(a => NegativeToMaxValue(Array.IndexOf(new[] { "y", "x" }, a)))
    .ThenBy(a => a);
于 2013-02-05T13:27:02.963 回答
1

没有内置的比较方法可以做你想做的事,但我猜这不是你所说的“冗长”部分。

令人讨厌的是,您必须创建一个自定义比较器类才能传递应该是一个简单的比较函数。

好吧,有一种方法可以减轻这种情况。您可以编写几个帮助类,让您只需传递一个方法的名称就可以使用 OrderBy()。如果您编写这些类,它们将适用于您的所有 OrderBy() 语句。

这是一些示例代码。辅助类称为 EnumerableExt 和 ComparisonDelegator。它们一起工作以允许您将方法传递给 OrderBy()。

下面的代码显然比您的代码长得多,但请记住,EnumerableExt 和 ComparisonDelegator 类将位于单独的公共程序集中,因此您不应计算它们。

using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;

namespace Demo
{
    public static class Program
    {
        private static void Main(string[] args)
        {
            var unorderered = new[] { "a", "b", "c", "x", "y", "z" };

            var ordered = unorderered.OrderBy(compare); // Just need to specify the compare method!
        }

        // Each custom compare method must be written specially, as before:

        private static int compare(string x, string y)
        {
            if (x == y)
                return 0;
            else
            {
                //----------------------------
                //beginning of custom ordering
                var customPriority = new[] { "y", "x" };
                if (customPriority.Any(a => a == x) && customPriority.Any(a => a == y)) //both in custom ordered array
                {
                    if (Array.IndexOf(customPriority, x) < Array.IndexOf(customPriority, y))
                        return -1;
                    return 1;
                }
                else if (customPriority.Any(a => a == x)) //only one item in custom ordered array (and its x)                    
                    return -1;
                else if (customPriority.Any(a => a == y)) //only one item in custom ordered array (and its y)                    
                    return 1;
                //---------------------------
                //degrade to default ordering
                else
                    return string.Compare(x, y);

            }
        }
    }

    // The following classes only need to be written once:

    public static class EnumerableExt
    {
        /// <summary>
        /// Convenience method on IEnumerable{T} to allow passing of a
        /// Comparison{T} delegate to the OrderBy method.
        /// </summary>

        public static IEnumerable<T> OrderBy<T>(this IEnumerable<T> list, Comparison<T> comparison)
        {
            Contract.Requires(list != null, "list can't be null.");
            Contract.Requires(comparison != null, "comparer can't be null.");

            return list.OrderBy(t => t, new ComparisonDelegator<T>(comparison));
        }
    }

    /// <summary>
    /// Provides a mechanism for easily converting a Comparison&lt;&gt; delegate (or lambda) to an IComparer&lt;&gt;.
    /// This can be used for List.BinarySearch(), for example.
    /// </summary>
    /// <typeparam name="T">The type of items to be compared.</typeparam>

    public sealed class ComparisonDelegator<T>: IComparer<T>, IComparer
    {
        /// <summary>Create from a Comparison&lt;&gt; delegate.</summary>
        /// <param name="comparison">A Comparison&lt;&gt; delegate.</param>

        public ComparisonDelegator(Comparison<T> comparison)
        {
            Contract.Requires(comparison != null);

            this._comparison = comparison;
        }

        /// <summary>Implements the IComparer.Compare() method.</summary>

        public int Compare(T x, T y)
        {
            return _comparison(x, y);
        }

        /// <summary>Implements the IComparer.Compare() method.</summary>

        public int Compare(object x, object y)
        {
            return _comparison((T)x, (T)y);
        }

        /// <summary>Used to store the Comparison delegate.</summary>

        private readonly Comparison<T> _comparison;
    }
}

然后,您还可以如下内联编写 compare 方法(但我不建议将这种方法用于如此复杂的 compare 方法;这只是为了说明):

    private static void Main(string[] args)
    {
        var unorderered = new[] { "a", "b", "c", "x", "y", "z" };

        var ordered = unorderered.OrderBy((x, y) =>
        {
            if (x == y)
                return 0;
            else
            {
                var customPriority = new[] { "y", "x" };
                if (customPriority.Any(a => a == x) && customPriority.Any(a => a == y)) //both in custom ordered array
                {
                    if (Array.IndexOf(customPriority, x) < Array.IndexOf(customPriority, y))
                        return -1;
                    return 1;
                }
                else if (customPriority.Any(a => a == x)) //only one item in custom ordered array (and its x)                    
                    return -1;
                else if (customPriority.Any(a => a == y)) //only one item in custom ordered array (and its y)                    
                    return 1;
                else
                    return string.Compare(x, y);
            }
        });
    }
于 2013-02-05T12:56:41.873 回答
0

我有 99.99% 的把握,在 .Net 框架中默认不存在这样的东西。


您的排序是非常自定义的,不是一种通用的排序方式,因此默认情况下,.NET Framework 中不存在类似的排序。

于 2013-02-05T12:36:58.250 回答