C # array lookup and sorting implementation code _ Practical skills

Source: Internet
Author: User
1. Find objects
Copy Code code as follows:

person P1 = new Person ("http://www.my400800.cn", 18);
person P2 = new Person ("http://www.my400800.cn", 19);
Person P3 = new Person ("http://www.my400800.cn", 20);
person[] Persons = ... {p1, p2, p3};
Find the position in the array where P2 is located
Array.indexof < person > (persons, p2);

2. Find values
Copy Code code as follows:

person P1 = new Person ("http://www.my400800.cn", 18);
person P2 = new Person ("http://blog.my400800.cn", 19);
Person P3 = new Person ("http://blog.my400800.cn/400 telephone", 20);
person[] Persons = ... {p1, p2, p3};
person P4 = new Person (p2. Name, p2. Age);
Find the location of the same element in the array as P4
Array.indexof < person > (persons, p4);

However, this method must enable the person to overload the Equals comparison method of the object
Copy Code code as follows:

public override bool Equals (object obj)
... {
person is = obj as person;
if (person = = null) return false;
Return (this. Name = = Person.name && this. Age = = Person.age);
}

The second method to find by object value
Implement Icomparabler interface
Copy Code code as follows:

public int CompareTo (object obj)
... {
person is = obj as person;
if (person = = null)
throw new Exception ("the method or operation isn't implemented.");
Start with the age comparison
int ageresult = this. Age.compareto (person.age);
if (Ageresult = 0)
... {
If the age equals in the sitting name comparison
return this. Name.compareto (Person.name);
}
Else
... {
return ageresult;
}
}

After implementing the IComparable interface, you can use Array.BinarySearch () to find the
Copy Code code as follows:

Get the subscript with the same value for person in persons
If multiple values are the same, BinarySearch will take the last
An array subscript with the same value
Array.BinarySearch < person > (persons, Person);

Note: You must manipulate a sorted array using Array.binaryseach
3. Sorting
As long as the object implements the IComparable interface, you can sort by using the static method sort in the array
Copy Code code as follows:

Must enable the object of comparison to implement the IComparable interface
Array.Sort < person > (persons);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.