Sort a list by a custom order

后端 未结 3 2029
忘了有多久
忘了有多久 2020-12-12 02:35

I have an array of some types

private string[] linkTypes = {
    \"dog\",
    \"cat\",
    // and so on ..
};

Yes, I could u

3条回答
  •  不思量自难忘°
    2020-12-12 03:04

    Comparisons such as "alphabetical order" (string) "bigger number" (numerical types) etc. are accomplished using the IComparable interface. You can implement this interface in your custom class to make instances sort themselves any way you like. Read up on the interface here:

    https://msdn.microsoft.com/en-us/library/4d7sx9hd(v=vs.110).aspx

    If you have a fixed number of types, then you could use a quick helper method to return an integer for each object depending on its type, and compare the returned integers from each object to determine which one "comes first."

提交回复
热议问题