我在CustomDataList类中创建了一些排序算法:
public void SortStudents() //sorts students alphabetically
{
string temp;
for (int i = 0; i < students.Count; i++)
{
for (int j = i + 1; j < students.Count; j++)
{
if (string.Compare(students[i], students[j]) > 0)
{
temp = students[j];
students[j] = students[i];
students[i] = temp;
}
}
Console.WriteLine(students[i]);
}
}
public string GetMaxElement()
{
string tempMax = null;
foreach (string element in students)
{
if (tempMax == null || string.Compare(tempMax, element) > 0)
{
tempMax = element;
}
}
return tempMax;
}
public string GetMinElement()
{
string tempMin = null;
foreach (string element in students)
{
if (tempMin == null || string.Compare(tempMin, element) < 0)
{
tempMin = element;
}
}
return tempMin;
}
}
Main()方法中的以下代码:
CustomDataList customDataList = new CustomDataList();
//Add(element)
customDataList.AddStudent("Jenny");
customDataList.AddStudent("Loren");
customDataList.AddStudent("Martin");
customDataList.AddStudent("Hannah");
customDataList.AddStudent("Joules");
customDataList.AddStudent("Daniel");
customDataList.AddStudent("Andy");
customDataList.AddStudent("Emilio");
customDataList.AddStudent("Ariana");
customDataList.AddStudent("Nikkita");
此代码工作正常,但现在我需要添加类似于SortStudents()方法(不使用.Sort()方法)的其他排序算法(例如,基于名称的长度对列表进行排序的算法)。 / p>
如果您可以提出其他排序方式以及有关如何创建该排序算法的一些提示,我将不胜感激。
答案 0 :(得分:0)
如果您没有义务使用常规数组操作,建议您使用linq
var result = customDataList.OrderBy(x => x.Length)
并用于按名称排序
var result = customDataList.OrderBy(x => x);
如果需要使用数组排序算法,请替换
if (string.Compare(students[i], students[j]) > 0)
使用
if (students[i].Length < students[j].Length)
答案 1 :(得分:0)
您可以使其更加面向对象。声明这样的学生班:
public class Student : IComparable<Student>
{
public int ID {get;set;}
public string Name {get;set;}
public int CompareTo(Student other)
{
return string.Compare(Name, other.Name);
}
}
现在,
List<Student> customDataList = new List<Student>();
customDataList.AddStudent("Jenny");
customDataList.AddStudent("Loren");
customDataList.AddStudent("Martin");
customDataList.AddStudent("Hannah");
customDataList.AddStudent("Joules");
customDataList.AddStudent("Daniel");
customDataList.AddStudent("Andy");
customDataList.AddStudent("Emilio");
customDataList.AddStudent("Ariana");
customDataList.AddStudent("Nikkita");
customDataList.Sort(); // sorts by name. default sorting
要实现多种排序,您可以执行以下操作: 实施IComparer接口:
public class NameLengthComparer: IComparer<string>
{
public int Compare(string x, string y)
{
if (x == null)
{
if (y == null)
{
// If x is null and y is null, they're
// equal.
return 0;
}
else
{
// If x is null and y is not null, y
// is greater.
return -1;
}
}
else
{
// If x is not null...
//
if (y == null)
// ...and y is null, x is greater.
{
return 1;
}
else
{
// ...and y is not null, compare the
// lengths of the two strings.
//
int retval = x.Length.CompareTo(y.Length);
if (retval != 0)
{
// If the strings are not of equal length,
// the longer string is greater.
//
return retval;
}
else
{
// If the strings are of equal length,
// sort them with ordinary string comparison.
//
return x.CompareTo(y);
}
}
}
}
}
然后致电
NameLengthComparer nc = new NameLengthComparer();
customDataList.Sort(nc); // sorts by length of Name
答案 2 :(得分:0)
如果您构建一个可靠的企业应用程序,并且想要支持多种类型的对象/排序算法/比较逻辑,则需要考虑其他答案所建议的适当的面向对象的IComparable
。您可能还希望将排序算法包装到抽象接口中。
但是,从角度来看,如果您只是进行实验,则可以使用一种更实用的方法来实现它,对于某些开发人员(例如对于JavaScript开发人员),它看起来更自然。
您可以传递Func
作为高阶函数,该函数负责比较对象的逻辑。它更短,可以让您将排序算法与任何种类的比较逻辑一起重用,而无需实现庞大的综合比较器类。
public void SortStudents(Func<string, string, int> compareFunction)
{
string temp;
for (int i = 0; i < students.Count; i++)
{
for (int j = i + 1; j < students.Count; j++)
{
if (compareFunction(students[i], students[j]) > 0)
{
temp = students[j];
students[j] = students[i];
students[i] = temp;
}
}
Console.WriteLine(students[i]);
}
}
SortStudents(string.Compare); // sorts students alphabetically
SortStudents((a, b) => a.Length.CompareTo(b.Length)); // sorts students by the length of their names
然后您可以在涉及比较的任何其他算法中重用此Func
:
public string GetMaxElement(Func<string, string, int> compareFunction)
{
string tempMax = null;
foreach (string element in students)
{
if (tempMax == null || compareFunction(tempMax, element) > 0)
{
tempMax = element;
}
}
return tempMax;
}
答案 3 :(得分:-1)
您可以使用IComparer
界面
public void SortStudents(IComparer<string> comparer) // sort students using bubble sort
{
string temp;
for (int i = 0; i < students.Count; i++)
{
for (int j = i + 1; j < students.Count; j++)
{
if (comparer.Compare(students[i], students[j]) > 0) // use given comparer instead.
{
temp = students[j];
students[j] = students[i];
students[i] = temp;
}
}
Console.WriteLine(students[i]);
}
}
排序方式
//sorts students alphabetically
customDataList.SortStudents(Comparer<string>.Create((a, b) => string.Compare(a, b)));
//sorts students by length
customDataList.SortStudents(Comparer<string>.Create((a, b) => a.Length.CompareTo(b.Length)));
基本上,您使用的是相同的排序算法,相同的代码,但IComparer接口提供的比较逻辑不同。
此技术称为“依赖注入”。在这种情况下是方法注入的类型。