2017-06-15 54 views
-1

我想排序一個名爲Sales的對象列表,但似乎每個人似乎都使用的方法沒有正確排序。下面的代碼是對列表進行排序的類,但是當我在單元測試中打印「排序」列表時,它顯示它根本沒有被排序。這種非常常見的排序方法怎麼不排序我的列表?

using System; 
using System.Collections; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
using System.Diagnostics; 


namespace ConsoleApplication1 
{ 
    /// <summary> 
    /// This class modifies an array of all of the sales in order to 
     manipulate the order and contents 
    /// which will then print out onto the excel document. 
    /// </summary> 
    public class Modifier 
    { 
     public List<Sales> salesList; 

     /// <summary> 
     /// This is the constructor for the Modifier object. 
     /// </summary> 
     /// <param name="sales"> Takes the list of all of the sales objects 
       created. </param> 
     public Modifier(List<Sales> list) 
     { 
      salesList = list; 
     } 

     /// <summary> 
     /// This method is called by Main to perform all operations within 
      this class. 
     /// </summary> 
     /// <returns> Returns true if exits correctly. </returns> 
     public void execute() 
     { 
      deleteTJ(); 
      deleteFifteens(); 

      Console.WriteLine("List contains count: " + 
      salesList.Count.ToString()); 
      sortMaterial(); 


     public void sortMaterial() 
     { 
      salesList = salesList.OrderBy(o => o.material).ToList(); 
     } 

的下面是我的測試:

[TestMethod] 
public void TestSort() 
{    
    // Add material names to the sales fields created. 
    sale1.material = "DEF123"; 
    sale2.material = "ABC123"; 
    sale3.material = "ABC456"; 
    sale4.material = "GHI123"; 
    sale5.material = "GHI223"; 
    sale6.material = "ZZZ999"; 
    sale7.material = "ABC124"; 
    sale8.material = "JKL111"; 
    sale9.material = "ACB123"; 
    sale10.material = "ABC124"; 

    // Add sales to the list of sales. 
    testList.Add(sale1); 
    testList.Add(sale2); 
    testList.Add(sale3); 
    testList.Add(sale4); 
    testList.Add(sale5); 
    testList.Add(sale6); 
    testList.Add(sale7); 
    testList.Add(sale8); 
    testList.Add(sale9); 
    testList.Add(sale10); 

    // Declare Modifier Object. 
    Modifier modifier = new Modifier(testList); 

    // Sort sales 1-10 by alphabetical order. 
    modifier.sortMaterial(); 

    // Print statements to view whole list in output screen 
    Console.WriteLine(testList[0].material.ToString()); 
    Console.WriteLine(testList[1].material.ToString()); 
    Console.WriteLine(testList[2].material.ToString()); 
    Console.WriteLine(testList[3].material.ToString()); 
    Console.WriteLine(testList[4].material.ToString()); 
    Console.WriteLine(testList[5].material.ToString()); 
    Console.WriteLine(testList[6].material.ToString()); 
    Console.WriteLine(testList[7].material.ToString()); 
    Console.WriteLine(testList[8].material.ToString()); 
    Console.WriteLine(testList[9].material.ToString()); 


    Assert.AreEqual("ABC123", testList[0].material); 
    Assert.AreEqual("ABC124", testList[1].material); 
    Assert.AreEqual("ABC124", testList[2].material); 
    Assert.AreEqual("ZZZ999", testList[9].material); 
} 
+1

該代碼應該做什麼? –

+0

@PatrickHofman它應該按字母順序對銷售進行排序 –

+0

並且'salesList.OrderBy(s => s.material)'不允許? –

回答

2

這裏的問題是,你似乎並不理解引用在C#中是如何工作的。這與範圍沒有任何關係。

引用類型是一種佔位符,指向一些其他地方的實際數據集。

所以,你與你的testList變量開始的,這點到List對象的地方:

testList -----> [List] 

然後你傳遞到修改的構造函數和修改有一個字段名爲salesList,到它分配傳入的參考。現在testListsalesList引用的是同一個列表:

testList -----> [List] 
       ^
    salesList/

然後調用sortMaterial(),它採用OrderByOrderBy保留原始列表不變並返回一個IEnumerable<T>,從中你創建一個新的列表,其中分配給salesList,所以現在testListsalesList不同列表:

testList ----> [original list] 

salesList ----> [sorted list] 

到此爲止,原始列表保持不變,這就是爲什麼你看到你看到的結果。

您可以通過sorting the list in-place按照您想要的方式工作,而不是使用原樣保留原始列表的方法。但是現在,當人們不需要時,人們往往會避免修改對象。所以我會建議將改變sortMaterial,使其返回排序列表:

testList = modifier.sortMaterial(); 

和你的代碼:

public List<Sales> sortMaterial() 
{ 
    salesList = salesList.OrderBy(o => o.material).ToList(); 

    return salesList; 
} 

然後,您可以在調用它的結果分配給您的testList變量不應該像你期待的那樣工作。