繁体   English   中英

每种可能的订购/分类组合的退货清单

[英]Return list in every possible ordered/sorted combination

我如何能够以每种可能的有序组合返回此列表? 我有这个名单,并希望返回不同的可能性排序的一个新的列表,如1,2,3,42,3,1,4等(所有的可能性)

 var list = new List<string>();
 list.Add("1");
 list.Add("2");
 list.Add("3");
 list.Add("4");

怎么做?

摘自CodeProject上有关列表排列迭代器的以下文章

public static IEnumerable<IList> Permutate(IList sequence, int count)
{
    if (count == 1) yield return sequence;
    else
    {
        for (int i = 0; i < count; i++)
        {
            foreach (var perm in Permutate(sequence, count - 1))
                yield return perm;
            RotateRight(sequence, count);
        }
    }
}

您可以使用如下方法:

var listPermutated = Permutate(list, list.Count);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM