我正在为大学建立我的实验室,而且我对.net框架是陌生的。我想对数据进行排序,但是对于本实验,我需要遵循某些规则,其中之一是我不能使用列表,它必须是数组。
这是我的菜单
1 - Sort by Employee Name (ascending)
2 - Sort by Employee Number (ascending)
3 - Sort by Employee Pay Rate (descending)
4 - Sort by Employee Hours (descending)
5 - Sort by Employee Gross Pay (descending)
6 - Exit
我在模型类中有一个数据,并且将数据存储在我的员工数组中。我短路了菜单2,但所有菜单都需要全局功能。
排序代码
for (int i = 1; i < employees.Length; i++)
{
Employee current = employees[i];
int j = i - 1;
for (; j >= 0 && current.GetNumber() > employees[j].GetNumber(); j--)
{
employees[j + 1] = employees[j];
}
employees[j + 1] = current;
}
foreach (Employee employee in employees)
{
Console.WriteLine(employee);
}
问题:我有什么办法可以使我得到一个功能 每个菜单的输出不同,因为这是我可以使用的标准 一种功能来完成所有菜单。
如果我需要任何帮助,请发表评论。
答案 0 :(得分:1)
您可以使用委托来解决您的问题。因此,您将拥有一种对数组进行排序的通用方法,该方法还将接受有关如何比较2名雇员的委托:
public Employee[] Sort(Employee[] employees, Func<Employee, Employee, bool> comparer)
{
for (int i = 1; i < employees.Length; i++)
{
Employee current = employees[i];
int j = i - 1;
for (; j >= 0 && comparer(current,employees[j]); j--)
{
employees[j + 1] = employees[j];
}
employees[j + 1] = current;
}
return employees;
}
用法的一个例子是
Sort(employees, (e1, e2) => e1.GetNumber() > e2.GetNumber())
或
Sort(employees, (e1, e2) => string.Compare(e1.Name, e2.Name) < 0)
答案 1 :(得分:1)
我更改了您的代码。一个人可以检索财产然后使用它:
public void Sort(Employee[] employees, string propertyName)
{
var desiredProperty = typeof(Employee).GetProperty(propertyName, BindingFlags.Public | BindingFlags.Instance);
PropertyInfo info = typeof(Employee).GetProperty(propertyName);
if (info == null) { return; }
for (int i = 1; i < employees.Length; i++)
{
Employee current = employees[i];
int j = i - 1;
int curValue = Convert.ToInt32(info.GetValue(current));
int prevValue = Convert.ToInt32(info.GetValue(employees[j]));
for (; j >= 0 && curValue > prevValue; j--)
{
employees[j + 1] = employees[j];
}
employees[j + 1] = current;
}
foreach (Employee employee in employees)
{
Console.WriteLine(info.GetValue(employee));
}
}
答案 2 :(得分:0)
创建扩展类
public static class OrderedEnumerable
{
public static IOrderedEnumerable<TSource> OrderBy<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer = null)
{
if (source == null)
{
throw new ArgumentNullException();
}
if (keySelector == null)
{
throw new ArgumentNullException();
}
if (comparer == null)
{
comparer = Comparer<TKey>.Default;
}
Comparison<TSource> comparer2 = (x, y) => comparer.Compare(keySelector(x), keySelector(y));
return new OrderedEnumerableImpl<TSource>(source, comparer2);
}
public static IOrderedEnumerable<TSource> OrderByDescending<TSource, TKey>(this IEnumerable<TSource> source, Func<TSource, TKey> keySelector, IComparer<TKey> comparer = null)
{
if (source == null)
{
throw new ArgumentNullException();
}
if (keySelector == null)
{
throw new ArgumentNullException();
}
if (comparer == null)
{
comparer = Comparer<TKey>.Default;
}
Comparison<TSource> comparer2 = (x, y) => comparer.Compare(keySelector(y), keySelector(x));
return new OrderedEnumerableImpl<TSource>(source, comparer2);
}
private class OrderedEnumerableImpl<TSource> : IOrderedEnumerable<TSource>
{
private readonly IEnumerable<TSource> Source;
private readonly Comparison<TSource> Comparer;
public OrderedEnumerableImpl(IEnumerable<TSource> source, Comparison<TSource> comparer)
{
Source = source;
Comparer = comparer;
}
public IOrderedEnumerable<TSource> CreateOrderedEnumerable<TKey>(Func<TSource, TKey> keySelector, IComparer<TKey> comparer, bool descending)
{
if (comparer == null)
{
comparer = Comparer<TKey>.Default;
}
Comparison<TSource> comparer2;
if (descending)
{
comparer2 = (x, y) =>
{
int result = Comparer(x, y);
if (result == 0)
{
result = comparer.Compare(keySelector(y), keySelector(x));
}
return result;
};
}
else
{
comparer2 = (x, y) =>
{
int result = Comparer(x, y);
if (result == 0)
{
result = comparer.Compare(keySelector(x), keySelector(y));
}
return result;
};
}
return new OrderedEnumerableImpl<TSource>(Source, comparer2);
}
public IEnumerator<TSource> GetEnumerator()
{
var source = Source.ToArray();
// ** Here you do the sorting! **
Array.Sort(source, Comparer);
for (int i = 0; i < source.Length; i++)
{
yield return source[i];
}
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}
}
并按如下方式使用
public class Employee
{
public int Number { get; set; }
public int GrossPay { get; set; }
public int Hours { get; set; }
public int PayRate { get; set; }
public string Name { get; set; }
}
void Main()
{
var Employee = new Employee[] { new Employee { Name = "acb", Number = 123 }, new Employee { Name = "nmo", Number = 456 }, new Employee { Name = "xyz", Number = 789 } };
var sortedEmpByNum = Employee.OrderBy(x => x.Number);// by number asc
var sortedEmpByNubDesc = Employee.OrderByDescending(x => x.Number); //by number desc
var sortedEmpByName = Employee.OrderBy(x => x.Name); //by name asc
//you shoud use this one
var finalResult = Employee.OrderBy(x => x.Name)
.ThenBy(x => x.Number)
.ThenByDescending(x => x.PayRate)
.ThenByDescending(x => x.Hours)
.ThenByDescending(x => x.GrossPay);
}
credit => custom-lambda-sort-extension