2011-06-16 126 views
31

在.NET中是否有一個用於Asc或Desc命令的Enum? 我需要在不同庫中使用排序概念,並且我想盡可能地使用鬆耦合。在.NET中是否存在用於Asc或Desc排序的Enum?

+1

你的意思的LINQ?以及爲什麼你需要一個枚舉(不會是一個布爾就夠了 - 只有兩個選項) – 2011-06-16 13:19:00

+2

我認爲他實際上是一個枚舉。這聽起來像他只是想要某些東西用於排序方向(即當用作參數時),如果它已經在Fx某處出現了。 – heisenberg 2011-06-16 13:20:50

+8

@YetAnotherGeek「只有兩個選項」永遠不是使用布爾值的有效藉口。布爾存儲真假。他們對其他任何東西都不明確 - 包括性別,上升和下降等。 – BrainSlugs83 2013-09-24 21:55:13

回答

25
+0

正確答案!有什麼更好的(可能是原子,簡單)庫使用?我將在存儲庫的界面中使用它,所以SortOrder是更好的選擇? – Custodio 2011-06-16 13:35:45

+2

由@andrecarluccis判斷優秀的答案'ListSortDirection'更好,如果你只想升序/降序 - 因爲可用性更多的目標。 – 2011-06-16 14:17:10

+1

我會說ListSortDirection,因爲另一個綁定到SqlClient。 – BrainSlugs83 2013-09-24 21:58:11

2

有兩個,我知道: SortDirectionSortOrder

一個快速需要注意的是,這些都在System.Web.UI.WebControlsSystem.Windows.Forms命名空間分別發現,所以有他們可能不適用的可能性對於你在做什麼語義上的

+1

你是對的。但是,正如您認爲這在語義上是錯誤的,因爲我沒有提及這些庫中的UI或表單。 – Custodio 2011-06-16 13:25:33

+0

我在想系統perharps裏面的東西。 – Custodio 2011-06-16 13:26:00

4

SortOrderListSortDirection是兩個有效的選擇,但要記住這一點:

ListSortDirection

  • 可以在.net版本1.1至4和silverlight中找到。
  • 排序順序是強制性的:ListSortDirection只有「升序」和「降序」選項,因此用戶也可以選擇其中的一個。

SortOrder:只在.NET版本3.5和4爲Silverlight不支持

  • 可用。
  • 排序順序是可選的:您也有「未指定」選項。
3

Windows.Forms.SortOrderData.SqlClient.SortOrder

經檢查第一有趣的觀點具有值:

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

,而第二個有價值觀:

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

可能是一個好主意,一致,特別是如果序列化。

33

.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 
    } 
    

編輯:另一個已經到來,因爲這最初發布。

  • 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

    還要注意,雖然'SortOrder'住在System.Data中。SqlClient命名空間,它由System.Data程序集定義。 – Dejan 2015-11-17 14:30:14

    +0

    查看這些我認爲DirectoryServices選項是最好的,如果你認爲一個命令必須存在。如果你想了解自然順序的概念,那麼SqlClient就更好了。然而,我不知道爲什麼有人會打擾最終發送消息「通過不以任何特定方式訂購Foo而訂購Foo」......也許如果您想要未指定意味着洗牌結果,這將是合理的。 – 2016-03-14 14:42:01

    +0

    再仔細看看,我剛剛意識到Active Directory服務不像文件系統目錄。 SqlClient的SortOrder在.NET Core中,所以是XmlSortOrder。顯然,如果你不希望在SqlClient的 – 2016-03-17 13:02:58

    相關問題