Does an Enum exist for Asc or Desc ordering?

前端 未结 5 619
后悔当初
后悔当初 2021-02-03 17:53

Is there an Enum natively in .NET for asceding or Desceding ordering?

I need to use the ordering concept in different libraries, and I want loose coupling as possible.

相关标签:
5条回答
  • 2021-02-03 18:23

    SortOrder and ListSortDirection are two valid choices but keep in mind this:

    ListSortDirection:

    • available in .net verions 1.1 to 4 and silverlight.
    • the sort order is mandatory: ListSortDirection has only "Ascending" and "Descending" options so the user has too choose one of them.

    SortOrder:

    • available only in .net versions 3.5 and 4. No support for silverlight.
    • sort order is optional: you also have the "Unspecified" option.
    0 讨论(0)
  • 2021-02-03 18:25

    Interesting point relating to Windows.Forms.SortOrder and Data.SqlClient.SortOrder

    Upon inspection the first has values:

    public enum SortOrder
    {
        None = 0,
        Ascending = 1,
        Descending = 2,
    }
    

    while the second has values:

    public enum SortOrder
    {
        Unspecified = -1,
        Ascending = 0,
        Descending = 1,
    }
    

    Probably a good idea to be consistent, especially if serialising.

    0 讨论(0)
  • 2021-02-03 18:32

    There are more than 8 sorting enums in .NET. It goes to show, even at Microsoft engineers will re-invent the wheel. It's also interesting how wildly the commenting practices and code style varies.

    Here are the ones I've found:

    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
      }
      

    Edit: Another has arrived since this was originally posted.

    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
      }
      
    0 讨论(0)
  • 2021-02-03 18:38
    • SortOrder in System.Data.SqlClient
    • ListSortDirection in System.ComponentModel
    0 讨论(0)
  • 2021-02-03 18:40

    There are two that I know of: SortDirection and SortOrder

    One quick caveat is that those are found in the System.Web.UI.WebControls and System.Windows.Forms namespaces, respectively, so there's the possibility that they might not apply for what you're doing semantically.

    0 讨论(0)
提交回复
热议问题