我想排序一個名爲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);
}
該代碼應該做什麼? –
@PatrickHofman它應該按字母順序對銷售進行排序 –
並且'salesList.OrderBy(s => s.material)'不允許? –