【问题标题】:What is the correct way to sort List <string> using C#?使用 C# 对 List <string> 进行排序的正确方法是什么?
【发布时间】:2023-04-03 13:00:02
【问题描述】:

我有 LIST1 和 LIST2 并且喜欢比较这两个列表。以下是我的条件..

    1-If LIST1 and LIST2 have the same items than add same items to LIST3
    2-If LIST1 doesnt contain LIST2 items than add different items to LIST4
    3-if LIST2 doesnt contain LIST1 items than add different items to LIST5

可以说我的结果如下所示取决于条件;

LIST1<string> = A,B,C,D
LIST2<string> = A,K,F,C
LIST3<string> = A,C
LIST4<string> = B,D
LIST5<string> = K,F

这是我的代码;

     foreach (string src in LIST1)
       {  
           foreach (string trg in LIST2)
            {
                if (LIST1.ToString() == LIST2.ToString())
                {
                    LIST3.Add(LIST1.ToString());
                }

               else
                {                       
                    LIST4.Clear();
                    foreach (string l3 in LIST1)
                    {
                        if (!LIST2.Contains(l3))
                            LIST4.Add(l3);
                    }

                   LIST5.Clear();
                    foreach (string l4 in LIST2)
                    {
                        if (!LIST1.Contains(l4))
                        {
                            LIST5.Add(l4);
                        }
                    }

                }

            }
        }

【问题讨论】:

  • 如果List1 = 1, 2, 2, 2, 3, 3 and List2 = 1, 1, 1, 2, 2, 4,程序需要做什么?您是否执行此操作:List3 =1 2 2 / List4 = 2 3 3 / List5 = 1 1 4。请参阅this image 以了解我的意思。在这种情况下 except() 将不起作用。

标签: c# list foreach


【解决方案1】:

一个快速的方法是:

var list3 = list1.Intersect(list2).ToList();
var list4 = list1.Except(list2).ToList();
var list5 = list2.Except(list1).ToList();

更新:如果您需要处理更大的列表(和/或必须在多个地方编写),您可以编写如下扩展方法:

public static Tuple<IEnumerable<T>, IEnumerable<T>, IEnumerable<T>> Diff<T>(
    this IEnumerable<T> first, IEnumerable<T> second)
{
    var intersection = new List<T>();
    var onlyInFirst = new HashSet<T>();
    var onlyInSecond = new HashSet<T>(second);

    foreach (var item in first)
    {
        if (onlyInSecond.Remove(item)) intersection.Add(item);
        else onlyInFirst.Add(item);
    }

    return Tuple.Create<IEnumerable<T>, IEnumerable<T>, IEnumerable<T>>
        (intersection, onlyInFirst, onlyInSecond);
}

此方法返回一个由三个IEnumerable&lt;T&gt;s 组成的元组,表示交集、仅在第一个集合中的项目集和仅在第二个集合中的项目集;分别。

用法:

var list1 = new[] { "A", "B", "C", "D" };
var list2 = new[] { "A", "K", "F", "C" };

var diff = list1.Diff(list2);
// diff.Item1 = A,C (intersection)
// diff.Item2 = B,D (only in first)
// diff.Item3 = K,F (only in second)

【讨论】:

    【解决方案2】:

    不确定这与排序有什么关系,但这里是每个条件的 Linq 语句:

    List3 = List1.Intersect(List2).ToList();
    List4 = List1.Where(l1 => !List2.Any(l2 => l2 == l1)).ToList();
    List5 = List2.Where(l2 => !List1.Any(l1 => l2 == l1)).ToList();
    

    正如 cmets Except 中指出的那样也可以:

    List4 = List1.Except(List2).ToList();
    List5 = List2.Except(List1).ToList();
    

    【讨论】:

    • 你应该使用Except 来处理最后两条语句
    猜你喜欢
    • 2019-06-19
    • 1970-01-01
    • 1970-01-01
    • 2017-11-02
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多