C# 使用类对象在一个方法中插入排序
我正在为大学建设实验室,我对.net framwork还不熟悉。我想对数据进行排序,但我需要遵循这个实验室的某些规则,其中之一是我不能使用列表,它必须是数组 这是我的菜单C# 使用类对象在一个方法中插入排序,c#,.net,C#,.net,我正在为大学建设实验室,我对.net framwork还不熟悉。我想对数据进行排序,但我需要遵循这个实验室的某些规则,其中之一是我不能使用列表,它必须是数组 这是我的菜单 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)
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
我在模型类中有一个数据,并将数据存储在employees数组中。我缩短了菜单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);
}
for(int i=1;i=0&¤t.GetNumber()>员工[j].GetNumber();j--)
{
员工[j+1]=员工[j];
}
员工[j+1]=当前;
}
foreach(员工中的员工)
{
Console.WriteLine(员工);
}
问:有没有什么方法可以使一个函数
每个菜单都有不同的输出,因为这是我可以使用的标准
一个功能完成所有菜单
如果需要我这边的任何信息,请发表意见。您可以使用代理来解决您的问题。因此,您将有一个通用方法对数组进行排序,该方法还将接受关于如何比较两名员工的委托:
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)=>string.Compare(e1.Name,e2.Name)<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 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));
}
}
public void排序(Employee[]employees,string propertyName)
{
var desiredProperty=typeof(Employee).GetProperty(propertyName,BindingFlags.Public | BindingFlags.Instance);
PropertyInfo=typeof(Employee).GetProperty(propertyName);
如果(info==null){return;}
对于(int i=1;i=0&&CurveValue>prevValue;j--)
{
员工[j+1]=员工[j];
}
员工[j+1]=当前;
}
foreach(员工中的员工)
{
Console.WriteLine(info.GetValue(employee));
}
}
您应该使用LINQ。感谢您的快速回复。我在互联网上找到了解决方案,但它在列表或LINQ中。但我正在寻找数组。我建议最好的解决方案是定义一个EmployeeComparer
类,该类实现IComparer(Of Employee)
,并允许您指定按哪个属性排序。然后,您只需创建该类的实例并指定所需的属性,然后将其传递给Array.Sort
。
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);
}
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));
}
}