如何排序A:列表LEmp =新列表(); [英] HOW TO SORT A : list LEmp = new list();
问题描述
您好,我正在为学校做一个项目。
我有一份雇主名单,一切都很好,但现在我必须对项目进行排序使用雇主代码列表。
我需要使用像shellort,bubblesort,直接插入这样的方法。
我已经看到了代码每个方法(shellsort,bubblesort,直接插入),但我不明白我的排序:/
你能解释我如何将列表转换为数组吗?
但我认为我的老师想要使用排序方法排序!
任何人都可以帮助我拜托?
谢谢
Hello, im doing a project for school.
I have a list with employers and everything is work fine, but now i have to sort the items of the list using the code of employer.
I need to use one method like shellsort, bubblesort, direct insertion.
I already saw the code of each method(shellsort,bubblesort,direct insertion), but i dont understand how i sort :/
Can you explain me how i convert a list to a array?
But i think that my teacher want the sort using a sort method!
Can anyone help me please?
Thank you
推荐答案
最简单的方法?不要自己写! :笑:
Easiest way? Don''t write your own! :laugh:
List<Employer> myList = new List<Employer>();
//Fill the list
myList.Sort((x, y) => Comparer.Default.Compare(x.Code, y.Code));
或者,如果你想保留原始列表的顺序:
Or, if you want to preserve the order of the original list:
Employer[] array = myList.ToArray();
Array.Sort(array);
List<Employer> sorted = array.ToList();
OriginalGriff有正确的想法,不要自己使用.Net Framework。
您也可以这样写:
OriginalGriff has the right idea, don''t roll your own use the .Net Framework.
You could also write it this way:
Employer.Sort((x,y)=>x.Code.CompareTo(y.Code));
....或者使用Linq:
....Or use Linq:
var q = from e in Employer
orderby e.Code, e.Name, e.Address, e.Phone
select e;
Employer[] sortedEmployers = q.ToArray();
如果允许LINQ并且您想要获得新的排序列表,您可以使用以下任何一种变体:
If LINQ is allowed and you want to get a new sorted list, you may use either of the variants below:
List<Employer> sortedList = (from e in list orderby e.Code select e).ToList();
List<Employer> sortedList = list.OrderBy(e => e.Code).ToList();
干杯
Andi
Cheers
Andi
这篇关于如何排序A:列表LEmp =新列表();的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!