简体   繁体   English

尝试按枚举属性对对象列表进行排序

[英]Trying to sort a list of objects by an enum properties

I have a data structure like the following: 我的数据结构如下:

public enum Direction
{
  NONE, // SHOULD NEVER BE THIS
  UP,
  DOWN,
  LEFT,
  RIGHT
};

public struct dataStruct
{
   public Direction direction;

  public int CompareTo(dataStruct other)
  {
    return this.direction.CompareTo(other.direction);
  }
}

I'll have a list of dataStruct, and I want to be able to sort it according to the Direction. 我将有一个dataStruct列表,我希望能够根据Direction对其进行排序。 So all the dataStruct with UP comes first, followed by DOWN, LEFT and finally RIGHT. 因此,所有带有UP的dataStruct都排在首位,其次是DOWN,LEFT和RIGHT。

I tried to sort it with the following code, 我尝试使用以下代码对其进行排序,

ListOfData.Sort(
  delegate(dataStruct obj1, dataStruct obj2)
  {
    return obj1.direction.CompareTo(obj2.direction);
  }
);

But this gives me an error. 但这给我一个错误。 Am I doing something wrong? 难道我做错了什么?

Edit: I get the following error ArgumentException: does not implement right interface System.Collections.Generic.Comparer`1+DefaultComparer[dataStruct].Compare (dataStruct x, dataStruct y) 编辑:我收到以下错误ArgumentException:无法实现正确的接口System.Collections.Generic.Comparer`1 + DefaultComparer [dataStruct] .Compare(dataStruct x,dataStruct y)

Add IComparable<dataStruct> to struct declaration. IComparable<dataStruct>添加到结构声明中。

public struct dataStruct : IComparable<dataStruct>

And just execute 然后执行

ListOfData.Sort()

Try this: 尝试这个:

var x = ListOfData.OrderByDescending
              (ListOfData => (Direction)Enum.Parse(typeof(Direction), ListOfData.Direction, true));

or simply 或简单地

var x = ListOfData.OrderByDescending(i=> i.Direction);

If you just need to sort it by the direction, this simple code works. 如果您只需要按方向对其进行排序,则可以使用此简单代码。 Why do you need a CompareTo ?: 为什么需要CompareTo

public class Program
{
    public void Main(string[] args)
    {
        List<dataStruct> objs = new List<dataStruct>(){
            new dataStruct{direction = Direction.DOWN, id=1},
            new dataStruct{direction = Direction.LEFT, id =2},
            new dataStruct{direction = Direction.UP, id =3},
        };

        var result = objs.OrderBy(x=>x.direction);

        foreach (var element in result)
        {
            Console.WriteLine ("{0}, {1}", element.direction, element.id);
        }
    }
}

public struct dataStruct
{
    public Direction direction;
    public int id;
    //other properties
}

public enum Direction
{
  NONE, // SHOULD NEVER BE THIS
  UP,
  DOWN,
  LEFT,
  RIGHT
};

You should create 您应该创建

public class DirectionComparer:IComparer<TypeOfData>
{
  public int Compare(dataStruct other)
  {
    return this.direction.CompareTo(other.direction);
  }
}

where TypeOfData is type of elements in ListOfData 其中TypeOfData是ListOfData中元素的类型

and pass instatnce of it to sort method. 并将其不变性传递给排序方法。

ListOfData.Sort(new DirectionComparer());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM