C#: 引用类型数组 比较/排序.

class类型数组值比较:

继承IEquatable<>:

//file1.cs


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PC
{

        class Person : IEquatable<Person>
        {
            public int Id { get; set; }
            public string FirstName { get; set; }
            public string LastName { get; set; }


            public override string ToString()
            {
                return "shihuamarryme";
            }


            public override bool Equals(Object obj)
            {
                Console.WriteLine("override--------------------->");
                if (obj == null)
                {
                    return base.Equals(obj);
                }

                return this.Equals(obj as Person);
            }

            public bool Equals(Person other)
            {

                Console.WriteLine("not override--------------------->");
                if (other == null)
                {
                    return base.Equals(other);
                }

                return (this.Id == other.Id && this.FirstName == other.FirstName && this.LastName == other.LastName);
            }

            //public override int GetHashCode()
            //{
            //    return this.Id.GetHashCode();
            //}
        }
}




//mian.cs
using System;
using System.Collections.Generic;
using System.Collections;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using PC;

namespace test
{

    class test
    {
        public static void Main(string[] args)
        {
            Person p1 = new Person { FirstName = "shihua", LastName = "MarryMe" };
            new Person { FirstName = "shihua", LastName = "MarryMe" };

            Person[] ps1 = { new Person { FirstName = "shihua", LastName = "MarryMe" },
                                     new Person { FirstName = "shihua", LastName = "MarryMe" },
                                     p1 };
            Person[] ps2 = { new Person { FirstName = "shihua", LastName = "MarryMe" },
                                       new Person { FirstName = "shihua", LastName = "MarryMe" },
                                    p1 };


            if ((ps1 as IStructuralEquatable).Equals(ps2, EqualityComparer<Person>.Default))
            {
                Console.WriteLine("is same");
            }


            return;
        }

    }

}

 

 

class类型数组排序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace test
{

    public class Demo : IComparable<Demo>
    {

        private string firstName_;

        public string FirstName
        {
            get
            {
                return this.firstName_;
            }
            set
            {
                this.firstName_ = value;
            }
        }

        public int CompareTo(Demo other)
        {
            if (other == null)
            {
                return 1;
            }

            int result = System.String.Compare(this.FirstName, other.FirstName);
            return result;
        }
    }


    class test
    {
        public static void Main(string[] args)
        {
            Demo[] array = { new Demo { FirstName = "x"} ,
                                        new Demo {FirstName = "a"},
                                        new Demo {FirstName = "c" },
                                        new Demo {FirstName = "b" } };

            Array.Sort(array);

            foreach(var val in array)
            {
                Console.WriteLine(val.FirstName);
            }

            return;

        }
    }

}

 

class类型数组排序:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace test
{

    public class Demo /*: IComparable<Demo>*/
    {

        private string firstName_;

        public string FirstName
        {
            get
            {
                return this.firstName_;
            }
            set
            {
                this.firstName_ = value;
            }
        }

        public int CompareTo(Demo other)
        {
            if (other == null)
            {
                return 1;
            }

            int result = System.String.Compare(this.FirstName, other.FirstName);
            return result;
        }
    }

    class CompareDemo : IComparer<Demo>
    {
        //private CompareDemo CompareType;

        //public CompareDemo(CompareDemo compareType)
        //{
        //    this.CompareType = compareType;
        //}

        public int Compare(Demo lh, Demo rh)
        {
            if(lh == null && rh == null)
            {
                return 0;
            }

            if(lh == null && rh != null)
            {
                return 0;
            }

            if(lh != null && rh == null)
            {
                return 0;
            }

            int result = System.String.Compare(lh.FirstName, rh.FirstName);
            return result;
        }
    }


    class test
    {
        public static void Main(string[] args)
        {
            Demo[] array = { new Demo { FirstName = "x"} ,
                                        new Demo {FirstName = "a"},
                                        new Demo {FirstName = "c" },
                                        new Demo {FirstName = "b" } };

            Array.Sort(array, new CompareDemo());

            foreach (var val in array)
            {
                Console.WriteLine(val.FirstName);
            }

            return;

        }
    }

}

 

转载于:https://my.oschina.net/SHIHUAMarryMe/blog/885455

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值