在.NET中是否有一个Enum本身用于升序或降序排序?
我需要在不同的库中使用排序概念,我希望尽可能松散耦合。
答案 0 :(得分:39)
.NET中有超过8个排序枚举。它表明,即使在微软工程师将重新发明轮子。同样有趣的是评论实践和代码风格各不相同。
以下是我发现的:
System.ComponentModel.ListSortDirection
public enum ListSortDirection {
/// <devdoc>
/// <para>Sort in ascending order.</para>
/// </devdoc>
Ascending,
/// <devdoc>
/// <para>Sort in descending order.</para>
/// </devdoc>
Descending
}
System.Data.SqlClient.SortOrder
public enum SortOrder {
Unspecified = -1,
Ascending = 0,
Descending = 1
}
System.Data.Linq.SqlClient.SqlOrderType
internal enum SqlOrderType {
Ascending,
Descending
}
System.DirectoryServices.SortDirection
public enum SortDirection
{
//
// Summary:
// Sort from smallest to largest. For example, A to Z.
Ascending,
//
// Summary:
// Sort from largest to smallest. For example, Z to A.
Descending
}
System.Windows.Forms.SortOrder
/// <include file='doc\SortOrder.uex' path='docs/doc[@for="SortOrder"]/*' />
/// <devdoc>
/// <para>
/// Specifies how items in
/// a list are sorted.
/// </para>
/// </devdoc>
public enum SortOrder {
/// <include file='doc\SortOrder.uex' path='docs/doc[@for="SortOrder.None"]/*' />
/// <devdoc>
/// <para>
/// The items are
/// not sorted.
/// </para>
/// </devdoc>
None = 0,
/// <include file='doc\SortOrder.uex' path='docs/doc[@for="SortOrder.Ascending"]/*' />
/// <devdoc>
/// <para>
/// The items
/// are sorted in ascending order.
/// </para>
/// </devdoc>
Ascending = 1,
/// <include file='doc\SortOrder.uex' path='docs/doc[@for="SortOrder.Descending"]/*' />
/// <devdoc>
/// <para>
/// The items are
/// sorted in descending order.
/// </para>
/// </devdoc>
Descending = 2,
}
System.Web.Helpers.SortDirection
public enum SortDirection {
Ascending,
Descending
}
System.Web.UI.WebControls.SortDirection
public enum SortDirection {
Ascending = 0,
Descending = 1
}
public enum XmlSortOrder {
Ascending = 1,
Descending = 2,
}
System.Data.Common.EntitySql.AST.OrderKind
/// <summary>
/// Represents order kind (none=asc,asc,desc).
/// </summary>
internal enum OrderKind
{
None,
Asc,
Desc
}
编辑:自最初发布以来,另一个人已经到了。
System.Web.UI.DataVisualization.Charting
/// <summary>
/// Sorting order (Ascending or Descending).
/// </summary>
public enum PointSortOrder
{
/// <summary>
/// Ascending sorting order
/// </summary>
Ascending,
/// <summary>
/// Descending sorting order
/// </summary>
Descending
}
答案 1 :(得分:30)
答案 2 :(得分:4)
SortOrder和ListSortDirection是两个有效的选择,但请记住:
答案 3 :(得分:3)
与Windows.Forms.SortOrder和Data.SqlClient.SortOrder
相关的有趣观点检查后,第一个有值:
public enum SortOrder
{
None = 0,
Ascending = 1,
Descending = 2,
}
而第二个有值:
public enum SortOrder
{
Unspecified = -1,
Ascending = 0,
Descending = 1,
}
保持一致可能是一个好主意,特别是在序列化时。
答案 4 :(得分:2)
我知道有两个: SortDirection 和 SortOrder
有一点需要注意的是,它们分别位于System.Web.UI.WebControls
和System.Windows.Forms
命名空间中,因此它们可能无法应用于您正在进行的操作语义