{"id":253826,"date":"2022-10-04T08:03:48","date_gmt":"2022-10-04T00:03:48","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=253826"},"modified":"2022-09-28T11:04:42","modified_gmt":"2022-09-28T03:04:42","slug":"result-linux-program","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/result-linux-program.html","title":{"rendered":"\u7b80\u5355\u4ecb\u7ecdC#\u4e2d\u5bf9\u96c6\u5408\u6392\u5e8f\u7684\u4e09\u79cd\u65b9\u5f0f"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u8fd9\u7bc7\u6587\u7ae0\u4ecb\u7ecd\u4e86C#\u4e2d\u5bf9\u96c6\u5408\u6392\u5e8f\u7684\u4e09\u79cd\u65b9\u5f0f\uff0c\u6587\u4e2d\u901a\u8fc7\u793a\u4f8b\u4ee3\u7801\u4ecb\u7ecd\u7684\u975e\u5e38\u8be6\u7ec6\u3002\u5bf9\u5927\u5bb6\u7684\u5b66\u4e60\u6216\u5de5\u4f5c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u501f\u9274\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e0b<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n

\u5bf9\u96c6\u5408\u6392\u5e8f\uff0c\u53ef\u80fd\u6700\u5148\u60f3\u5230\u7684\u662f\u4f7f\u7528OrderBy\u65b9\u6cd5\u3002<\/p>\n

class Program\r\n{\r\n    static void Main(string[] args)\r\n    {\r\n        IEnumerable result = GetStudents().OrderBy(r => r.Score);\r\n        foreach (var item in result)\r\n        {\r\n            Console.WriteLine(item.Name + \"--\" + item.Score);\r\n        }\r\n        Console.ReadKey();\r\n    }\r\n    private static List<\/student> GetStudents()\r\n    {\r\n        return new List<\/student>()\r\n        {\r\n            new Student(){Id = 1, Name = \"\u5f20\u4e09\",Age = 15, Score = 80},\r\n            new Student(){Id = 2, Name = \"\u674e\u56db\",Age = 16, Score = 70},\r\n            new Student(){Id = 3, Name = \"\u8d75\u6b66\",Age = 14, Score = 90}\r\n        };\r\n    }\r\n}\r\npublic class Student \r\n{\r\n    public int Id { get; set; }\r\n    public string Name { get; set; }\r\n    public int Age { get; set; }\r\n    public int Score { get; set; }\r\n}<\/student><\/pre>\n

\u4ee5\u4e0a\uff0cOrderBy\u8fd4\u56de\u7684\u7c7b\u578b\u662fIEnumerable\u3002<\/p>\n

\u5982\u679c\u60f3\u4f7f\u7528List\u7684Sort\u65b9\u6cd5\uff0c\u5c31\u9700\u8981\u8ba9Student\u5b9e\u73b0IComparable\u63a5\u53e3\u3002<\/p>\n

class Program\r\n {\r\n     static void Main(string[] args)\r\n     {\r\n         List result = GetStudents();\r\n         result.Sort();\r\n         foreach (var item in result)\r\n         {\r\n             Console.WriteLine(item.Name + \"--\" + item.Score);\r\n         }\r\n         Console.ReadKey();\r\n     }\r\n     private static List<\/student> GetStudents()\r\n     {\r\n         return new List<\/student>()\r\n         {\r\n             new Student(){Id = 1, Name = \"\u5f20\u4e09\",Age = 15, Score = 80},\r\n             new Student(){Id = 2, Name = \"\u674e\u56db\",Age = 16, Score = 70},\r\n             new Student(){Id = 3, Name = \"\u8d75\u6b66\",Age = 14, Score = 90}\r\n         };\r\n     }\r\n }\r\n public class Student : IComparable<\/student>\r\n {\r\n     public int Id { get; set; }\r\n     public string Name { get; set; }\r\n     public int Age { get; set; }\r\n     public int Score { get; set; }\r\n      \r\n     public int CompareTo(Student other)\r\n     {\r\n       return  this.Score.CompareTo(other.Score);\r\n     }\r\n }<\/student><\/pre>\n

\u8ba9Student\u5b9e\u73b0IComparable<\/student>\u63a5\u53e3\u56fa\u7136\u5f88\u597d\uff0c\u5982\u679cStudent\u662f\u4e00\u4e2a\u5bc6\u5c01\u7c7b\uff0c\u6211\u4eec\u65e0\u6cd5\u8ba9\u5176\u5b9e\u73b0IComparable<\/student>\u63a5\u53e3\u5462\uff1f\u4e0d\u7528\u62c5\u5fc3\uff0cSort\u65b9\u6cd5\u63d0\u4f9b\u4e86\u4e00\u4e2a\u91cd\u8f7d\uff0c\u53ef\u4ee5\u63a5\u6536IComparer\u63a5\u53e3\u7c7b\u578b\u3002<\/p>\n

class Program\r\n {\r\n     static void Main(string[] args)\r\n     {\r\n         List result = GetStudents();\r\n         result.Sort(new StudentSorter());\r\n         foreach (var item in result)\r\n         {\r\n             Console.WriteLine(item.Name + \"--\" + item.Score);\r\n         }\r\n         Console.ReadKey();\r\n     }\r\n     private static List<\/student> GetStudents()\r\n     {\r\n         return new List<\/student>()\r\n         {\r\n             new Student(){Id = 1, Name = \"\u5f20\u4e09\",Age = 15, Score = 80},\r\n             new Student(){Id = 2, Name = \"\u674e\u56db\",Age = 16, Score = 70},\r\n             new Student(){Id = 3, Name = \"\u8d75\u6b66\",Age = 14, Score = 90}\r\n         };\r\n     }\r\n }\r\n public class Student\r\n {\r\n     public int Id { get; set; }\r\n     public string Name { get; set; }\r\n     public int Age { get; set; }\r\n     public int Score { get; set; }\r\n }\r\n public class StudentSorter : IComparer<\/student>\r\n {\r\n     public int Compare(Student x, Student y)\r\n     {\r\n         return x.Score.CompareTo(y.Score);\r\n     }\r\n }<\/student><\/pre>\n

\u7efc\u4e0a\uff0c\u5982\u679c\u6211\u4eec\u60f3\u5bf9\u4e00\u4e2a\u96c6\u5408\u6392\u5e8f\uff0c\u5927\u81f4\u6709\u4e09\u79cd\u65b9\u5f0f\uff1a<\/p>\n

  • 1\u3001\u4f7f\u7528OrderBy\u65b9\u6cd5\uff0c\u8fd4\u56deIEnumerable\u7c7b\u578b\u3002<\/t><\/li>\n
  • 2\u3001\u8ba9\u96c6\u5408\u5143\u7d20\u5b9e\u73b0IComparable\u63a5\u53e3\uff0c\u518d\u4f7f\u7528Sort\u65b9\u6cd5\uff0c\u8fd4\u56devoid\u3002<\/t><\/li>\n
  • 3\u3001\u96c6\u5408\u5143\u7d20\u4e0d\u5b9e\u73b0IComparable\u63a5\u53e3\uff0c\u9488\u5bf9\u96c6\u5408\u5143\u7d20\u7c7b\u578b\u5199\u4e00\u4e2a\u5b9e\u73b0IComparer<\/t>\u63a5\u53e3\u7684\u7c7b\uff0c\u628a\u8be5\u7c7b\u5b9e\u4f8b\u4f5c\u4e3aSort\u65b9\u6cd5\u7684\u53c2\u6570\u3002<\/t><\/li>\n

    <\/student><\/t><\/student><\/p>\n","protected":false},"excerpt":{"rendered":"

    \u5bf9\u96c6\u5408\u6392\u5e8f\uff0c\u53ef\u80fd\u6700\u5148\u60f3\u5230\u7684\u662f\u4f7f\u7528OrderBy\u65b9\u6cd5\u3002 class Program { static void […]<\/p>\n","protected":false},"author":362,"featured_media":4213,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-253826","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/253826","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/362"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=253826"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/253826\/revisions"}],"predecessor-version":[{"id":253829,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/253826\/revisions\/253829"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/4213"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=253826"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=253826"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=253826"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}