我正在尝试对名为Sales的对象列表进行排序,但是每个人似乎都使用的方法没有正确地为我排序。下面的代码是对列表进行排序的类,但是当我打印" sorted"单元测试中的列表显示它根本没有排序。
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);
}
答案 0 :(得分:2)
这里的问题是你似乎不了解引用如何在C#中工作。这与范围没有任何关系。
引用类型是一种占位符,指向其他地方的实际数据集。
所以你开始使用testList
变量,并指向某个List对象:
testList -----> [List]
然后将其传递给Modifier的构造函数,并且Modifier有一个名为salesList
的字段,它为其分配传入的引用。现在testList
和salesList
引用了相同的列表:
testList -----> [List]
^
salesList /
然后拨打sortMaterial()
,使用OrderBy
。 OrderBy
保留原始列表不变,并返回IEnumerable<T>
,您可以从中创建新列表,并将其分配给salesList
,现在{{1} }和testList
引用不同的列表:
salesList
到目前为止,原始列表仍然保持不变,这就是您看到所见结果的原因。
您可以通过sorting the list in-place以您希望的方式使代码工作,而不是使用保持原始列表不变的方法。但是现在,人们倾向于避免在他们不必要时修改物体。所以我建议改变 testList ----> [original list]
salesList ----> [sorted list]
以便它返回排序列表:
sortMaterial
然后,您可以在调用时将结果分配给public List<Sales> sortMaterial()
{
salesList = salesList.OrderBy(o => o.material).ToList();
return salesList;
}
变量:
testList
并且您的代码不应该像您一样期待。