Sort a list by a custom order

后端 未结 3 2031
忘了有多久
忘了有多久 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:07

    Implement either the IComparer or the IComparable interface. The downside of using the IComparable is that this has to be implemented by the class which is targeted for sorting, which means that in case you want to sort it a different way elsewhere in your code, you will not be able to do so using this mechanism. On the other hand, IComparer can be decoupled from the target class and implemented in multiple ways if you choose to, and depending on the sorting criteria in different parts of your application, you could apply one of these IComparer classes as needed.

    https://support.microsoft.com/en-us/help/320727/how-to-use-the-icomparable-and-icomparer-interfaces-in-visual-c

提交回复
热议问题