通过首先在列表中查找要对其进行排序的字段来对列表进行排序

时间:2013-05-17 22:20:39

标签: c# .net silverlight

这个似乎很难,并希望得到任何人可能给予的帮助或帮助。

我有一个列表,列表。每个Car对象都有一个列表List。我想按照这些配件中的一个来排序我的汽车清单。在我的例子中,我保证有几个Car对象带有我想要找到的附件。

现在我知道附件名称,但我不知道用于在附件名称上对我的Car列表进行排序的语法。

  • AccessoryObj1 =>名称(字符串:动态转向)
  • AccessoryObj2 =>名称(字符串:辅助协助)

  • Car1 - >列表(AccessoryObj1,AccessoryObj2,AccessoryObj3)

  • Car2 - >列表(AccessoryObj1,AccessoryObj2, AccessoryObj3)

列表(Car1,Car2)

我尝试的语法如下:

String sortField = "Side Assist";
List<Car> sortedList = sortedList.OrderBy(
                        x => x.Accessories.OfType<Accessory>().ToList().First(y => y.Name == sortField)).ToList();

我试图通过引用每个Car对象的附件列表来OrderBy(升序)我的汽车列表,并进行First匹配,其中附件列表中的附件对象名称与sortField值匹配。

不幸的是,此尝试会导致以下错误:

  

至少有一个对象必须实现IComparable

我希望我的问题很清楚,并提前感谢。


感谢用户2399170提供的解决方案,我的方式是3/4:采取他们的解决方案/样本,我现在正在尝试将最终结果命令为升序/降序。这是调整后的代码:

public class Accessory
{
    public string Name { get; set; }
    public string Value { get; set; }
}

public class Car
{
    public string Name { get; set; }
    public IList<Accessory> Accessories { get; set; }
}

private IEnumerable<Car> GetCars()
    {
        var cars = new List<Car>();
        cars.Add(
            new Car()
            {
                Name = "CAR1",
                Accessories = new List<Accessory>()
                {
                    new Accessory() { Name = "ABS Brakes", Value = "1" },
                    new Accessory() { Name = "Airbag", Value = "1" },
                    new Accessory() { Name = "Climate Control", Value = "1" },
                    new Accessory() { Name = "Heated Seats", Value = "1" },
                    new Accessory() { Name = "HID Xenon Headlights", Value = "1" },
                    new Accessory() { Name = "Radio", Value = "1" },
                    new Accessory() { Name = "Quattro", Value = "0" },
                    new Accessory() { Name = "Sat/Nav", Value = "1" },
                    new Accessory() { Name = "Side Assist", Value = "0" },
                    new Accessory() { Name = "Name", Value = "BMW" },
                }
            });
        cars.Add(
            new Car()
            {
                Name = "CAR2",
                Accessories = new List<Accessory>()
                {
                    new Accessory() { Name = "ABS Brakes", Value = "1" },
                    new Accessory() { Name = "Airbag", Value = "1" },
                    new Accessory() { Name = "Climate Control", Value = "1" },
                    new Accessory() { Name = "Heated Seats", Value = "0" },
                    new Accessory() { Name = "HID Xenon Headlights", Value = "0" },
                    new Accessory() { Name = "Radio", Value = "1" },
                    new Accessory() { Name = "Quattro", Value = "0" },
                    new Accessory() { Name = "Sat/Nav", Value = "0" },
                    new Accessory() { Name = "Side Assist", Value = "0" },
                    new Accessory() { Name = "Name", Value = "Mazda" },
                }
            });
        cars.Add(
            new Car()
            {
                Name = "CAR3",
                Accessories = new List<Accessory>()
                {
                    new Accessory() { Name = "ABS Brakes", Value = "1" },
                    new Accessory() { Name = "Airbag", Value = "1" },
                    new Accessory() { Name = "Climate Control", Value = "1" },
                    new Accessory() { Name = "Heated Seats", Value = "1" },
                    new Accessory() { Name = "HID Xenon Headlights", Value = "1" },
                    new Accessory() { Name = "Radio", Value = "1" },
                    new Accessory() { Name = "Quattro", Value = "1" },
                    new Accessory() { Name = "Sat/Nav", Value = "1" },
                    new Accessory() { Name = "Side Assist", Value = "1" },
                    new Accessory() { Name = "Name", Value = "Audi" },

                }
            });

        String[] sortRules = new string[] { "Name" };

        List<Car> sortedCars = sortRules
            .SelectMany(sortRule => cars.Where(car => car.Accessories.Any(acc => acc.Name == sortRule)))
            .Distinct().ToList();

        return sortedCars;
    }

我想根据排序字段'Name'的值来排序最终结果,在这个例子中,“Audi”,“BMW”,“Mazda”

这一切的工作原理是,用户可以从“下拉名称”中进行选择,结果可以按“名称”的值进行排序。不幸的是,这是我无法控制的第三方来源返回数据的方式。

2 个答案:

答案 0 :(得分:0)

要按Accessory排序,您需要确保该类实现IComparable接口

public class Accessory : IComparible
{
    public int CompareTo(object obj) {
        //Return 0 for equals
        //Return -1 if this object precedes the given object
        //Return 1 if this object follows the given object
    }
}

答案 1 :(得分:0)

试试这个样本

private static IEnumerable<Car> GetCars(string filterByAccessory, bool sortAscending = true)
{
    var cars = new List<Car>();
    cars.Add(
        new Car()
        {
            Name = "CAR1",
            Accessories = new List<Accessory>()
        {
            new Accessory() { Name = "ABS Brakes", Value = "1" },
            new Accessory() { Name = "Airbag", Value = "1" },
            new Accessory() { Name = "Climate Control", Value = "1" },
            new Accessory() { Name = "Heated Seats", Value = "1" },
            new Accessory() { Name = "HID Xenon Headlights", Value = "1" },
            new Accessory() { Name = "Radio", Value = "1" },
            new Accessory() { Name = "Quattro", Value = "0" },
            new Accessory() { Name = "Sat/Nav", Value = "1" },
            new Accessory() { Name = "Side Assist", Value = "0" },
            new Accessory() { Name = "Name", Value = "BMW" },
        }
        });
    cars.Add(
        new Car()
        {
            Name = "CAR2",
            Accessories = new List<Accessory>()
        {
            new Accessory() { Name = "ABS Brakes", Value = "1" },
            new Accessory() { Name = "Airbag", Value = "1" },
            new Accessory() { Name = "Climate Control", Value = "1" },
            new Accessory() { Name = "Heated Seats", Value = "0" },
            new Accessory() { Name = "HID Xenon Headlights", Value = "0" },
            new Accessory() { Name = "Radio", Value = "1" },
            new Accessory() { Name = "Quattro", Value = "0" },
            new Accessory() { Name = "Sat/Nav", Value = "0" },
            new Accessory() { Name = "Side Assist", Value = "0" },
            new Accessory() { Name = "Name", Value = "Mazda" },
        }
        });
    cars.Add(
        new Car()
        {
            Name = "CAR3",
            Accessories = new List<Accessory>()
        {
            new Accessory() { Name = "ABS Brakes", Value = "1" },
            new Accessory() { Name = "Airbag", Value = "1" },
            new Accessory() { Name = "Climate Control", Value = "1" },
            new Accessory() { Name = "Heated Seats", Value = "1" },
            new Accessory() { Name = "HID Xenon Headlights", Value = "1" },
            new Accessory() { Name = "Radio", Value = "1" },
            new Accessory() { Name = "Quattro", Value = "1" },
            new Accessory() { Name = "Sat/Nav", Value = "1" },
            new Accessory() { Name = "Side Assist", Value = "1" },
            new Accessory() { Name = "Name", Value = "Audi" },

        }
        });

    var filteredCars = cars.Where(c => c.Accessories.Any(a => a.Name == filterByAccessory));

    var sortedCars = sortAscending
        ? filteredCars.OrderBy(x => x.Accessories.First(a => a.Name == filterByAccessory).Value)
        : filteredCars.OrderByDescending(x => x.Accessories.First(a => a.Name == filterByAccessory).Value);

    return sortedCars;
}