user2661591
user2661591

Reputation: 113

What is the correct way to sort List <string> using C#?

I have LIST1 <> and LIST2 <> and like to compare these two lists. Followings are my conditions..

    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

lets say my result is like below depends on the conditions;

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

here is my code;

     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);
                        }
                    }

                }

            }
        }

Upvotes: 1

Views: 112

Answers (2)

Eren Ers&#246;nmez
Eren Ers&#246;nmez

Reputation: 39085

A quick way to do this would be:

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

Update: If you have to do with larger lists (and/or have to write this in multiple places), you can write an extension method like below:

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);
}

This method returns a tuple of three IEnumerable<T>s representing the set of intersection, set of items only in the first collection, and set of items only in the second collection; respectively.

Usage:

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)

Upvotes: 5

D Stanley
D Stanley

Reputation: 152556

Not sure what this has to do with sorting, but here's Linq statements for each condition:

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();

as pointed out in comments Except will work too:

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

Upvotes: 0

Related Questions