是否存在用于Asc或Desc排序的枚举?

时间:2011-06-16 13:17:29

标签: c# .net

在.NET中是否有一个Enum本身用于升序或降序排序?

我需要在不同的库中使用排序概念,我希望尽可能松散耦合。

5 个答案:

答案 0 :(得分:39)

.NET中有超过8个排序枚举。它表明,即使在微软工程师将重新发明轮子。同样有趣的是评论实践和代码风格各不相同。

以下是我发现的:

  1. System.ComponentModel.ListSortDirection

    public enum ListSortDirection {
        /// <devdoc>
        ///    <para>Sort in ascending order.</para>
        /// </devdoc>
        Ascending,
        /// <devdoc>
        ///    <para>Sort in descending order.</para>
        /// </devdoc>
        Descending 
    }
    
  2. System.Data.SqlClient.SortOrder

    public enum SortOrder {
        Unspecified     = -1,
        Ascending       = 0,
        Descending      = 1
    }
    
  3. System.Data.Linq.SqlClient.SqlOrderType

    internal enum SqlOrderType {
        Ascending,
        Descending
    }
    
  4. 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
    }
    
  5. 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,
    
    }
    
  6. System.Web.Helpers.SortDirection

    public enum SortDirection {
        Ascending,
        Descending
    }
    
  7. System.Web.UI.WebControls.SortDirection

    public enum SortDirection {
    
    
        Ascending = 0,
    
    
        Descending = 1
    
    }
    
  8. System.Xml.XPath.XmlSortOrder

    public enum XmlSortOrder {
        Ascending       = 1,
        Descending      = 2,
    }
    
  9. System.Data.Common.EntitySql.AST.OrderKind

    /// <summary>
    /// Represents order kind (none=asc,asc,desc).
    /// </summary>
    internal enum OrderKind
    {
        None,
        Asc,
        Desc
    }
    
  10. 编辑:自最初发布以来,另一个人已经到了。

    1. 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)

SortOrderListSortDirection是两个有效的选择,但请记住:

ListSortDirection

  • .net verions 1.1 to 4和silverlight。
  • 排序顺序是强制性的:ListSortDirection只有“升序”和“降序”选项,所以用户也选择其中一个。

SortOrder

  • 仅适用于.net版本3.5和4.不支持silverlight。
  • 排序顺序是可选的:您还有“未指定”选项。

答案 3 :(得分:3)

Windows.Forms.SortOrderData.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.WebControlsSystem.Windows.Forms命名空间中,因此它们可能无法应用于您正在进行的操作语义