2011-03-28 97 views
18

我有喜歡類:排序列表

class SortNode 
{ 
    public Int32 m_valRating = 0; 

    public SortNode(Int32 valRating) 
    { 
     this.m_valRating = valRating; 
    } 
} 

和一些列表refSortNodeList

 List<SortNode> refSortNodeList = new List<SortNode>(); 

     Random refRandom = new Random(); 

     for (int i = 0; i < 100; ++i) 
     { 
      refSortNodeList.Add(new SortNode(refRandom.Next(-10, 30))); 
     } 

     foreach (var varSortNode in refSortNodeList) 
     { 
      Console.WriteLine("SortNode rating is {0}", varSortNode.m_valRating); 
     } 

如何輕鬆地我refSortNodeList通過m_valRating場排序?或者,也許我需要使用另一個List類?

謝謝!!!

+2

公共字段是一個壞主意,順便說一句 – 2011-03-28 15:45:20

回答

49
list.Sort((x,y) => 
    x.m_valRating.CompareTo(y.m_valRating)); 
+0

+1就是這樣一個:) – 2011-03-28 15:44:40

+2

如果它比較null,則會拋出。 – 2011-03-28 15:47:10

+2

@Jon - 你當然可以在那裏編碼,但是這裏的空值可能是錯誤的 – 2011-03-28 15:49:15

1

它易於使用LINQ:

var newlist = refSortNodeList.sort(n => n.m_valRating); 
+0

錯誤的方法,錯誤的資本。 – 2011-03-28 15:46:35

+0

你說得對。我混淆了OrderBy() – dcarneiro 2011-03-28 15:49:18

+0

'Sort()'不返回值,'OrderBy()'不返回列表。 – mgronber 2011-03-28 15:50:35

7

通過使用LINQ順序。

var mySortedList = refSortNodeList.OrderBy(x => x.m_valRating); 

這裏是一個真實的例子,我從數據庫中拉出一個列表,但它是完全相同的概念。

vendorProducts = (from vp in db.COMPANIES_VND_PRODUCTS 
        join p in db.CT_CT_INV_CLASSES on vp.CLASS_ID equals p.CLASS_ID 
        join m in db.CT_CT_MODALITY_CODES on vp.MODALITY_ID equals m.MODALITY_ID 
        where vp.COMPANY_ID == companyId 
        select new ProductTypeModality 
        { 
         Active = p.ACTIVE.Equals("Y") ? true : false, 
         BioMedImaging = p.BIOMED_IMAGING, 
         Code = p.CLASS_CODE, 
         Description = p.DESCRIPTION, 
         Id = p.CLASS_ID, 
         PricingMargin = p.PRICING_MARGIN, 
         ModalityCode = m.MODALITY_CODE, 
         ModalityId = m.MODALITY_ID, 
         VendorId = companyId 
        }).OrderBy(x => x.Code).ToList<ProductTypeModality>(); 
1

您可以使用LINQ的基本排序:如果您需要更復雜的排序

refSortNodeList.OrderBy(n => n.m_valRating); 

你將需要實現IComparable使用內置的排序。

10

就地:

refSortNodeList.Sort(
    (x, y) => 
    x == null ? (y == null ? 0 : -1) 
     : (y == null ? 1 : x.m_valRating.CompareTo(y.m_valRating)) 
); 

創建一個新的枚舉:

var newEnum = refSortNodeList.OrderBy(x => x.m_valRating); 

創建一個新的列表:

var newList = refSortNodeList.OrderBy(x => x.m_valRating).ToList(); 

就地是最快和最高效存儲,但如果你還想保留舊的名單,那也不好。

下一個比上一個更快,並提供結果,但你必須重新做這種再次使用它,在這種情況下,第三個是要去的。

+0

+1,因爲它處理'空' – 2011-03-28 16:07:11

1

試試這個:

refSortNodeList.Sort(new delgate(SortNode x, SortNode y) 
    { 
     return x.CompareTo(y); 
    } 
); 
0
List<SortNode> refSortNodeList = new List<SortNode>(); 

Random refRandom = new Random(); 

for (int i = 0; i < 100; ++i) { 
    refSortNodeList.Add (new SortNode (refRandom.Next (-10, 30))); 
} 

// Use this (Linq) if you're using .NET 3.5 or above. 
var sortedList = refSortNodeList.OrderBy (node => node.m_valRating); 
foreach (var varSortNode in sortedList) { 
    Console.WriteLine ("SortNode rating is {0}", varSortNode.m_valRating); 
} 

// Use this otherwise (e.g. .NET 2.0) 
refSortNodeList.Sort (
    delegate (SortNode n1, SortNode n2) { 
     return n1.m_valRating.CompareTo (n2.m_valRating); 
    } 
); 

foreach (var varSortNode in refSortNodeList) { 
    Console.WriteLine ("SortNode rating is {0}", varSortNode.m_valRating); 
}