| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 752 人关注过本帖
标题:求教C#初级问题
只看楼主 加入收藏
zerghuman
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2012-7-27
收藏
 问题点数:0 回复次数:4 
求教C#初级问题
我的代码可以按照数据中的一个元素实现升序或者降序的排序, 如果要通过任一搜索两个对应元素来排序该怎么做啊?!   
代码如下:using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace CustomInterfaces
{
    class Address : ICloneable
    {
        public string StreetAddress { get; set; }
        public string City { get; set; }
        public string State { get; set; }
        public string ZipCode { get; set; }

        #region ICloneable Members
        public object Clone()
        {
            Address anew = (Address)this.MemberwiseClone();
            return anew;
            
        }
        #endregion
    }
}


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

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

        private void button1_Click(object sender, EventArgs e)
        {
            Student s1 = new Student
            {
                FirstName = "Bill",
                LastName = "Baker",
                Id =12356,
                Test1Score =85,
                Test2Score = 91
            };

            Student s2 = s1; // copies references only
            //MessageBox.Show(s2.ToString());
            s1.FirstName = "Adam";
            //MessageBox.Show(s2.ToString());

            //proper copying through cloning
            s1.TestScores[0] = 87;
            s1.TestScores[1] = 92;
            
            Student s3 = (Student)s1.Clone();
            MessageBox.Show(s3.ToString() +"\n"+
                s3.TestScores[0].ToString());

            s1.FirstName = "Mark";
            s1.TestScores[0] = 120;

            MessageBox.Show(s3.ToString() + "\n" +
                s3.TestScores[0].ToString());
        }

        private void button2_Click(object sender, EventArgs e)
        {
            ArrayList STList = new ArrayList();
            Student s1=new Student
            {
                FirstName="Bill", LastName="Baker",
                Test1Score=85, Test2Score=91,
                Id=12345
            };
            STList.Add(s1);

            Student s2 = new Student
            {
                FirstName = "Sally",
                LastName = "Simpson",
                Test1Score = 89,
                Test2Score = 93,
                Id = 12348
            };
            STList.Add(s2);

            Student s3 = new Student
            {
                FirstName = "Mark",
                LastName = "Williams",
                Test1Score = 81,
                Test2Score = 87,
                Id = 12347
            };
            STList.Add(s3);
            STList.Sort();  // will use IComparable to
                            // decide field to sort on

            string out1 = "";
            foreach (Student st in STList)
                out1 += st.ToString() + "\n";
            MessageBox.Show(out1);

        }

        private void button3_Click(object sender, EventArgs e)
        {
            ArrayList STList = new ArrayList();
            Student s1 = new Student
            {
                FirstName = "Bill",
                LastName = "Baker",
                Test1Score = 85,
                Test2Score = 91,
                Id = 12345
            };
            STList.Add(s1);

            Student s2 = new Student
            {
                FirstName = "Sally",
                LastName = "Simpson",
                Test1Score = 89,
                Test2Score = 93,
                Id = 12348
            };
            STList.Add(s2);

            Student s3 = new Student
            {
                FirstName = "Mark",
                LastName = "Williams",
                Test1Score = 81,
                Test2Score = 87,
                Id = 12347
            };
            STList.Add(s3);

            Student s4 = new Student
            {
                FirstName = "James",
                LastName = "Williams",
                Test1Score = 81,
                Test2Score = 77,
                Id = 12346
            };
            STList.Add(s4);

            StudentComparer sc=new StudentComparer();
            //sc.FieldName = "LNAME";
            //sc.FieldName = SORTFIELD.STUDENTID;
            //sc.SortOrder = SORTORDER.DESCENDING;

            STList.Sort(sc);  // will use IComparable to
            // decide field to sort on

            string out1 = "";
            foreach (Student st in STList)
                out1 += st.ToString() + "\n";
            MessageBox.Show(out1);
        }
    }
}



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

namespace CustomInterfaces
{
    enum SORTFIELD : int
    {
        FIRSTNAME,
        LASTNAME,
        STUDENTID,
        TELEPHONE,
        TEST1,
        TEST2
    }

    enum SORTORDER : int
    {
        ASCENDING,
        DESCENDING
    }
}



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

namespace First
{
            // c# like Java has single class inheritance but
        // multiple interface inheritance
    class Student : ICloneable, IComparable
    {
        // in good OOP, keep data private
        // and expose it to the outside via
        // get and set methods
        // C# gives us properties instead of
        // get and set methods

        Address addr=new Address();
        public Address Addr
        {
            get { return addr; }
            set { addr = value; }
        }

        int[] testScores = new int[3];
        public int[] TestScores
        {
            get { return testScores; }
            set { testScores = value; }
        }

        string firstName;
        public string FirstName
        {
            get { return firstName; }
            set { firstName = value; }
        }

        string lastName;
        public string LastName
        {
        get { return lastName;}
        set {lastName = value;}
        }

        int telePhone;
        public int TelePhone
        {
            get { return telePhone; }
            set { telePhone = value; }
        }

        int id;
        public int Id
        {
            get { return id; }
            set { id = value; }
        }

        int test1Score;
        public int Test1Score
        {
            get { return test1Score; }
            set { test1Score = value; }
        }

        int test2Score;
        public int Test2Score
        {
            get { return test2Score; }
            set { test2Score = value; }
        }

        // in good OOP, methods are usually public
        public string ComputeGrade()
        {
            double avg = 0.4 * test1Score +
                0.6 * test2Score;
            string grade = ""; // good practice to
            // initialize variables as you declare them
            if (avg > 90)
                grade = "A";
            else if (avg > 85)
                grade = "A-";
            else if (avg > 80)
                grade = "B+";
            else
                grade = "B";
            return grade;
        }

        public override string ToString()
        {
            return firstName + " : " + lastName + " : "
                + id.ToString() + " : " +
                test1Score.ToString() + " : " +
                test2Score.ToString(); ;
        }

        #region ICloneable Members
        public object Clone()
        {
            Student snew = new Student();
            //snew.firstName = this.firstName;
            //snew.lastName = this.lastName;
            snew = (Student)this.MemberwiseClone();
            // 这是上两行乃至多行的简写
            // above will not copy reference types
            // such as arrays properly
            // need to call clone explicitly on each reference
            // type in the class
            snew.testScores = (int [])this.testScores.Clone();
            snew.Addr = (Address)this.Addr.Clone();
            return snew;
            
        }
        #endregion

        #region IComparable Members
        // return 1 0 -1
        public int CompareTo(object obj)
        {
            int res = 0;
            Student s2 = null;
            if (obj is Student)
            {
                s2 = (Student)obj;
                res = this.(s2.lastName);
                //res = res * -1;   相反按从大到小排列
            }
            return res;
         }
        #endregion

        public int CompareTo(Student st, SORTFIELD sField)
        {
            int res = 0;
            switch (sField)
            {
                case SORTFIELD.FIRSTNAME:
                    res=this.(st.firstName);
                    break;
                case SORTFIELD.LASTNAME:
                    res=this.(st.lastName);
                    break;
                case SORTFIELD.STUDENTID:
                    res = this.(st.id);
                    break;
                case SORTFIELD.TELEPHONE:
                    res = this.(st.telePhone);
                    break;
                case SORTFIELD.TEST1:
                    res=this.(st.test1Score);
                    break;
                case SORTFIELD.TEST2:
                    res = this.(st.test2Score);
                    break;
            }
            return res;
        }
    }
}



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

namespace CustomInterfaces
{
    class StudentComparer: IComparer
    {
        //string fieldName; // field to sort on
        //public string FieldName
        //{
        //    get { return fieldName; }
        //    set { fieldName = value; }
        //}
        SORTFIELD fieldName1= SORTFIELD.STUDENTID;
        internal SORTFIELD FieldName1
        {
            get { return fieldName1; }
            set { fieldName1 = value; }
        }

        SORTFIELD fieldName2 = SORTFIELD.STUDENTID;
        internal SORTFIELD FieldName2
        {
            get { return fieldName2; }
            set { fieldName2 = value; }
        }

        SORTORDER sortOrder=SORTORDER.ASCENDING;
        internal SORTORDER SortOrder
        {
            get { return sortOrder; }
            set { sortOrder = value; }
        }

        #region IComparable Members      
        // returns 0, 1 or -1
        public int Compare(object x,object y)
        {
            int res = 0;
            Student s1 = null;
            Student s2 = null;
            if ((x is Student)&&(y is Student))
            {
                s1 = (Student)x;
                s2 = (Student)y;
            }
            res = (s2, fieldName1);
            if (sortOrder == SORTORDER.DESCENDING)
                res = -1 * res;
            return res;
        }
        #endregion
    }
}

搜索更多相关主题的帖子: 数据 public 元素 
2012-07-27 14:09
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
收藏
得分:0 
不是太明白你想要实现什么要求,你现在是可以按以下元素中的一个,在STList中,对Student类进行排序
FIRSTNAME, LASTNAME, STUDENTID, TELEPHONE, TEST1, TEST2
按两个元素排序是想要做什么
2012-07-27 14:49
zerghuman
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2012-7-27
收藏
得分:0 
回复 2楼 yhlvht
嗯,怪我说的不够清楚, 就是要按照STUDENT 中 任意一个元素来升序或者降序输出,但其中如果LastName有相同的,则将该两个对象的输出顺序对调
比如,按一个ID排序  输出结果是:
赵红 1 97
黎明 2 84
赵亮 3 90


先按ID 然后成绩就是:
赵亮 3 90
黎明 2 84
赵红 1 97


[ 本帖最后由 zerghuman 于 2012-7-27 15:32 编辑 ]
2012-07-27 15:18
yhlvht
Rank: 13Rank: 13Rank: 13Rank: 13
等 级:贵宾
威 望:36
帖 子:707
专家分:4405
注 册:2011-9-30
收藏
得分:0 
虽然我还是没有明白先按ID,再按成绩会是这样的:
先按ID 然后成绩就是:
赵亮 3 90
黎明 2 84
赵红 1 97
但是我知道你要做什么了。
排序的关键当然是StudentComparer类中Compare方法的实现。
你可以在你的窗体Form1中,用一个变量保存你之前的排序字段,下一次排序的时候,把上一次排序的字段传到StudentComparer类中,这样你就知道上一次是用什么排序的,这一次用什么排序,这样的话你的Compare方法就可以根据你的规则来写了。
2012-07-27 15:55
zerghuman
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2012-7-27
收藏
得分:0 
回复 4楼 yhlvht
大概明白了,我去尝试一下, 争取晋升为C#大白
2012-07-27 19:37
快速回复:求教C#初级问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.025487 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved