求教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
}
}