C#-需要更好的方法来将一个列表与另一个列表进行排序

我有两个列表-来宾列表和VIP列表.我需要对“来宾”列表进行排序,以便如果它包含VIP列表中的第一个人,那么他们将进入列表的顶部,依此类推. VIP列表用尽后,其余的来宾列表将保持原始顺序.排序必须同时使用名字和姓氏.我已经使用List和foreach语句完成了此操作,但是似乎应该有一种更优雅的方法.

有没有一种更简单,更现代的方式来进行这种处理?

class Guest 
{
    public int NumberInParty { get; set; }
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

class VIP
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

class TrackedGuest
{
    public Guest guest;
    public bool isTaken;

    public TrackedGuest(Guest g)
    {
        this.guest = g;
        isTaken = false;
    }
}
static void Main(string[] args)
{
    List<Guest> guests = new List<Guest>();

    guests.Add(new Guest { FirstName = "Rob", LastName = "Carson", NumberInParty = 5 });
    guests.Add(new Guest { FirstName = "George", LastName = "Waverly", NumberInParty = 3 });
    guests.Add(new Guest { FirstName = "Pete", LastName = "Spacely", NumberInParty = 2 });
    guests.Add(new Guest { FirstName = "George", LastName = "Jetson", NumberInParty = 6 });
    guests.Add(new Guest { FirstName = "Cosmo", LastName = "Spacely", NumberInParty = 2 });

    List<VIP> vips = new List<VIP>();
    vips.Add(new VIP { FirstName = "George", LastName = "Jetson" });
    vips.Add(new VIP { FirstName = "Cosmo", LastName = "Spacely" });

    List<TrackedGuest> TrackedGuests = new List<TrackedGuest>();

    foreach (Guest g in guests)
    {
        TrackedGuests.Add(new TrackedGuest(g));
    }

    List<Guest>SortedGuests = new List<Guest>();

    // Copy each guest on the VIP list in order
    foreach (VIP vip in vips)
    {
        foreach (TrackedGuest tGuest in TrackedGuests)
        {
            if (
                (tGuest.isTaken == false) &&
                (vip.FirstName == tGuest.guest.FirstName) &&
                (vip.LastName == tGuest.guest.LastName)
                )
            {
                SortedGuests.Add(tGuest.guest);
                tGuest.isTaken = true;
            }
        }        
    }

    // Process the rest of the guests
    if (SortedGuests.Count < guests.Count)
    {
        foreach (TrackedGuest tGuest in TrackedGuests)
        {
            if (tGuest.isTaken == false)
            {
                SortedGuests.Add(tGuest.guest);
                tGuest.isTaken = true;
            }
        }
    }

    foreach (Guest guest in SortedGuests)
    {
        Console.WriteLine(guest.FirstName + " " + guest.LastName + ": " + guest.NumberInParty + " in party.");

    }

    Console.ReadLine();
}

解决方法:

var sorted = new List<Guest>();
var guestvips = from g in guests
                from v in vips.Where(vip => vip.FirstName == g.FirstName && vip.LastName == g.LastName).DefaultIfEmpty()
                where v != null
                select g;
var guestsimple = from g in guests
                  from v in vips.Where(vip => vip.FirstName == g.FirstName && vip.LastName == g.LastName).DefaultIfEmpty()
                  where v == null
                  select g;

sorted.AddRange(guestvips.Concat(guestsimple));

此代码在VIP上两次“左加入”来宾.在第一种情况下,要接受具有相等vip的那些来宾,然后是没有相等vip的那些来宾.第一种情况实际上可以用’join’关键字重写.

上一篇:c#-通过反射通过自定义属性对实体进行Linq排序


下一篇:java-如何按行自然顺序和列插入顺序对番石榴表进行排序