Sorting an array alphabetically in C#

后端 未结 7 905
孤街浪徒
孤街浪徒 2020-12-09 17:12

Hope someone can help. I have created a variable length array that will accept several name inputs. I now want to sort the array in alphabetical order and return that to the

相关标签:
7条回答
  • 2020-12-09 17:49

    Create a comparer class

    class StudentComparer : IComparer<Student>
    {
        public int Compare(Student a, Student b)
        {
            return a.Name.CompareTo(b.Name);
        }
    }
    

    Sort:

    Array.Sort(students,new StudentComparer());
    
    0 讨论(0)
提交回复
热议问题