How to categorize values using the Sort method of the. NET Array class

Source: Internet
Author: User
Tags array arrays contains integer interface sort tostring

In the previous column, I discussed. NET array of basic functions. This week, we expanded this topic to focus on the classification of array content. The array class provides a sort method that has a variety of uses. I'll start with a simple approach and end with a custom category.

  Sort method

The static sort method using the array class is the easiest way to categorize the contents of an array. List A describes how to apply this method by categorizing a set of names.

It generates the following results, which show the list of names before and after the calling classification method:

0. Howard, Ryan

1. Allen, Ray.

2. Pujols, Albert

3. Iverson, Allen

0. Allen, Ray.

1. Howard, Ryan

2. Iverson, Allen

3. Pujols, Albert

  Here is the corresponding vb.net code:

Dim x as Integer

Dim names (3) as String

Names (0) = "Howard, Ryan"

Names (1) = "Allen, Ray"

Names (2) = "Pujols, Albert"

Names (3) = "Iverson, Allen"

For x = 0 to (names. LENGTH-1)

Console.WriteLine (CSTR (x) + "." + names (x). ToString ())

Next x

Array.Sort (names)

For x = 0 to (names. LENGTH-1)

Console.WriteLine (CSTR (x) + "." + names (x). ToString ())

Next x

The sort method of the array class can classify the array contents in various forms. Now that you understand the simplest form of application of this method, here are some other uses:

Sort (array, array); The one-to-one dimension array (one containing the key and the other containing the corresponding item) is categorized according to the keys in the first array.

Sort (Array, IComparable): classifies elements in a one-dimensional array with the specified IComparable interface.

Sort (array, array, IComparable): A one-to-one dimension array (one containing the key and the other containing the corresponding item) is categorized with the specified IComparable interface based on the keys in the first array.

Sort (array, Integer, integer): A one-dimensional array that specifies an element in a part of the beginning and end position (an integer value) to classify.

Sort (array, array, Integer, integer): A one-to-one dimension array (one containing the key and the other containing the corresponding item) is categorized according to the keys in the first array.

Sort (Array, Integer, Integer, IComparable): Sorts the elements in a one-dimensional array with the specified IComparable interface.

Sort (array, array, Integer, Integer, IComparable): The specified IComparable interface classifies a one-to-one dimension array (one containing the key and the other containing the corresponding item) from the key in the first array.

You can use the default behavior of the array class to classify an entire array and a portion of an array; You can also specify a detailed classification method through a special IComparable interface. The example in list B uses two arrays, one array contains keys and the other contains the corresponding items to categorize. It is the same as the first example of adding an array of keys.

The resulting result is an array value sorted according to the value in the key array (the second array is specified when the sort method is called). In list C is the corresponding vb.net code.

You can tweak the code a little bit, call the appropriate sort method, and make it categorize only one object. The following code uses the previous example, but only the second and third elements in the array are categorized.

Int[] keys = new INT[4];

Keys[0] = 11;

KEYS[1] = 3;

KEYS[2] = 8;

KEYS[3] = 5;

string[] names = new String[4];

Names[0] = "Howard, Ryan";

NAMES[1] = "Allen, Ray";

NAMES[2] = "Pujols, Albert";

NAMES[3] = "Iverson, Allen";

Array.Sort (keys, names, 1, 2);

  Here is the corresponding vb.net code:

Dim keys (3) as Integer

Keys (0) = 11

Keys (1) = 3

Keys (2) = 8

Keys (3) = 5

Dim names (3) as String

Names (0) = "Howard, Ryan"

Names (1) = "Allen, Ray"

Names (2) = "Pujols, Albert"

Names (3) = "Iverson, Allen"

Array.Sort (keys, names, 1, 2)

  Categorize Custom Objects

Although the simple sort method is convenient, you cannot use it to automatically categorize the array of custom data types. After all, you can't expect. NET platform to understand all the established objects! However, you can still use the IComparer or IComparable interfaces to illustrate the categorization of custom objects.

These interfaces provide you with a way to compare object instances, making sorting more convenient. The main difference between the two interfaces is that the comparison method must be included in the class when using the IComparable interface, and it is not necessary to use IComparer. The use details of two interfaces are not covered in this article, but I will use the IComparable interface to classify a custom class array.

In List D, I use the IComparable interface to process the classification logic and its classes. As the code shows, I set up a person class. Note that it uses the IComparable interface and uses the CompareTo method to compare (when sorting).

In the next example, this class is used to create a person array object. (Required by the IComparable interface) The CompareTo method is the key to categorizing the person class, which compares the object being submitted to itself. Next, call the array's sort method and use the classification mechanism of the person class to categorize the items in the array based on the first name attribute of each object in the arrays. List E is the person object that is used and categorized. List f is the corresponding vb.net code, which first lists the person class.

  Various options

Any data seems to be sorted. After all, people like to see the data displayed logically. NET Array class provides a sort method that simplifies the categorization of the array contents using its value or a separate array of key values. Alternatively, you can use other. NET features such as the IComparable interface to customize the classification.



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.