class Pxu : IComparable
    {
        public string FirstName;
        public string LastName;
        //public string FirstName
        //{
        //    get;
        //    set;
        //}
        //public string LastName
        //{
        //    get;
        //    set;
        //}
        public Pxu(string str1, string str2)
        {
            this.FirstName = str1;
            this.LastName = str2;
        }
        public int CompareTo(object o)
        {
            Pxu other = o as Pxu;
            int result= this.FirstName.CompareTo(other.FirstName);
            if (result == 0)
                result = this.LastName.CompareTo(other.LastName);
            return result;
        }  
    }        Pxu[] p ={
                        new Pxu("ww","eee"),
                        new Pxu("ytet","sdfer"),
                        new Pxu("uyte","dfeh"),
                    };
            Array.Sort(p);
            foreach (Pxu px in p)
            {
                Console.WriteLine(px);
            }输出得不到我想要的结果 我想要的结果当然是排序而这个输出的是ConsoleApplication2.PxuConsoleApplication2.PxuConsoleApplication2.Pxu输出三次。就解答为什么会是这样

解决方案 »

  1.   

    排序是正确的。输出不对。foreach (Pxu px in p)
    {
      Console.WriteLine(px.FirstName + px.LastName);
    }要么你重写Pxu类的ToString()
      

  2.   


    public Pxu(string str1, string str2)
      {
      this.FirstName = str1;
      this.LastName = str2;
      }
      public int CompareTo(object o)
      {
      Pxu other = o as Pxu;
      int result= this.FirstName.CompareTo(other.FirstName);
      if (result == 0)
      result = this.LastName.CompareTo(other.LastName);
      return result;
      }
      public override string ToString()
      {
          return string.Format("FirstName:{0},LastName:{1}", this.FirstName, this.LastName);
      }
      } 
    Pxu[] p ={
      new Pxu("ww","eee"),
      new Pxu("ytet","sdfer"),
      new Pxu("uyte","dfeh"),
      };
                Array.Sort(p);
                foreach (Pxu px in p)
                {
                    Console.WriteLine(px.ToString());
                }
      

  3.   

    本帖最后由 caozhy 于 2011-06-02 14:21:05 编辑