结构体数组中的某个成员进行排序

 


    class Program
    {
        static void Main(string[] args)
        {
                  

                 Struct[] array = new  Struct[3];
                     array[0] = new Struct();
                     array[0].A = 4;
                     array[0].B = "1";
                     array[1] = new Struct();
                     array[1].A = 3;
                     array[1].B = "2";
                     array[2] = new Struct();
                     array[2].A = 5;
                     array[2].B = "3";
                     Array.Sort(array, new MyComparer());
                     foreach (Struct s in array)
                     {
                         Console.WriteLine("{0},{1}", s.A, s.B);
                     }

            Console.ReadKey();
        }
        public struct Struct
        {
            public int A;
            public string B;
        }
        public class MyComparer : IComparer
        {
            #region IComparer 成员

            int IComparer.Compare(Struct x, Struct y)
            {
                return x.A - y.A;
            }

            #endregion
        }
    }

你可能感兴趣的:(C#-结构体)