简单的List排序

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wqq1027/article/details/84334880

1.实体中继承IComparable<T>

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

namespace Test
{
    class TestEntity : IComparable<TestEntity>
    {
        public double Double { get; set; }
        public int Index { get; set; }

        int IComparable<TestEntity>.CompareTo(TestEntity other)
        {
            if (other.Double > this.Double)
            {
                return 1;
            }
            else if (other.Double < this.Double)
            {
                return -1;
            }
            else
            {
                if (other.Index > this.Index)
                {
                    return -1;
                }
                else if (other.Index < this.Index)
                {
                    return 1;

                }
                else
                {
                    return 0;
                }
            }
        }

        public override string ToString()
        {
            return "进度是:" + this.Double + "  索引是:" + this.Index;
        }
    }
}

2.调用实体继承的Sort()方法

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Test
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            List<TestEntity> TestEntityList = new List<TestEntity>();

            TestEntity TestEntity1 = new TestEntity();
            TestEntity TestEntity2 = new TestEntity();
            TestEntity TestEntity3 = new TestEntity();
            TestEntity TestEntity4 = new TestEntity();
            TestEntity TestEntity5 = new TestEntity();
            TestEntity TestEntity6= new TestEntity();

            TestEntity1.Double = 0.7;
            TestEntity2.Double = 0.5;
            TestEntity3.Double = 0.5;
            TestEntity4.Double = 0.9;
            TestEntity5.Double = 0.7;
            TestEntity6.Double = 1.0;

            TestEntity1.Index = 1;
            TestEntity2.Index = 1;
            TestEntity3.Index = 1;
            TestEntity4.Index = 1;
            TestEntity5.Index = 0;
            TestEntity6.Index = 0;

            TestEntityList.Add(TestEntity1);
            TestEntityList.Add(TestEntity2);
            TestEntityList.Add(TestEntity3);
            TestEntityList.Add(TestEntity4);
            TestEntityList.Add(TestEntity5);
            TestEntityList.Add(TestEntity6);
            Console.WriteLine("------------------排序之前------------------");
            ConsoleTE(TestEntityList);
            TestEntityList.Sort();
            Console.WriteLine("------------------排序之后------------------");
            ConsoleTE(TestEntityList);
        }

        private static void ConsoleTE(List<TestEntity> TestEntityList)
        {
            foreach (TestEntity te in TestEntityList)
            {
                Console.WriteLine(te.ToString());
            }
        }
    }
}

结果:

------------------排序之前------------------
进度是:0.7  索引是:1
进度是:0.5  索引是:1
进度是:0.5  索引是:1
进度是:0.9  索引是:1
进度是:0.7  索引是:0
进度是:1  索引是:0
------------------排序之后------------------
进度是:1  索引是:0
进度是:0.9  索引是:1
进度是:0.7  索引是:0
进度是:0.7  索引是:1
进度是:0.5  索引是:1
进度是:0.5  索引是:1

猜你喜欢

转载自blog.csdn.net/wqq1027/article/details/84334880