按其他集合值对集合进行排序

时间:2015-07-28 09:29:25

标签: c# .net sorting collections

我有List<SomeData> data;

public class SomeData
{
    public int Key { get; set;}
    public decimal Value { get; set;}
}

我还有List<int> DataOrder;

我需要按List<SomeData>dataKey进行排序,将其置于与List<int> DataOrder值相同的位置。

有没有常见的算法?

示例:

List<SomeData> data = new List<SomeData>();
data.Add(new SomeData{ Key = 10, Value = 14 })
data.Add(new SomeData{ Key = 25, Value = 22 })
data.Add(new SomeData{ Key = 567, Value = 3 })
data.Add(new SomeData{ Key = 57, Value = 300 })
data.Add(new SomeData{ Key = 17, Value = 200 })
data.Add(new SomeData{ Key = 343, Value = 42 })

List<int> DataOrder = new List<int>{1, 25, 700, 567, 343, 350, 10};

排序后的结果:

foreach(var element in data)
{
     Console.WriteLine(element.Key);
}

输出:

25
567
343
10
57
17

修改:初始data数组可以包含Key,不包含在DataOrder中 此值应以任何顺序放在结果收集的末尾。 示例已更改以说明它。

3 个答案:

答案 0 :(得分:2)

加入怎么样:

var mySortedList = (from i in DataOrder
            join d in data on i equals d.Key
            select new SomeData 
            { 
                Key = d.Key, 
                Value = d.Value 
            });

编辑:要同时添加data中不共享DataOrder - 列表中任何键的值,您只需在结果中添加Union,如下所示:

var result = mySortedList.Union(data.Where(x => !DataOrder.Contains(x.Key)));

答案 1 :(得分:1)

解决

public class SomeData
{
    public int Key { get; set; }
    public decimal Value { get; set; }
}

class Program
{
    static void Main(string[] args)
    {
        List<SomeData> orders = new List<SomeData>();

        orders.Add(new SomeData { Key = 10, Value = 14 });
        orders.Add(new SomeData { Key = 25, Value = 22 });
        orders.Add(new SomeData { Key = 567, Value = 3 });
        orders.Add(new SomeData { Key = 57, Value = 300 });
        orders.Add(new SomeData { Key = 17, Value = 200 });
        orders.Add(new SomeData { Key = 343, Value = 42 });

        List<int> ids = new List<int> { 1, 25, 700, 567, 343, 350, 10 };

        //get orders only from ids with order
        List<SomeData> existedOrders = (from order in orders
                                        join id in ids
                                             on new { onlyId = order.Key }
                                            equals new { onlyId = id }
                                        orderby ids.IndexOf(id)
                                        select order).ToList();

        //add others
        existedOrders.AddRange(orders.Except(existedOrders).ToList());

    }
}

//使用@HimBromBeere解决方案,您可以减少查询

//get orders only from ids with order
List<SomeData> existedOrders = (from order in orders
                                join id in ids
                                     on order.Key equals id
                                orderby ids.IndexOf(id)
                                select order).ToList();

答案 2 :(得分:0)

let bottomConstraint = NSLayoutConstraint(item: myWebView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: myWebView, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0)

myWebView.addConstraint(bottomConstraint)

和交换功能用于项目的交换位置。