PROGRAMMING:Jmu-java-04 advanced object oriented -- 02 interface comparator
Arrays. Sort can sort all objects that implement comparable. However, if there are multiple sorting requirements, for example, sometimes you need to sort name in descending order, sometimes you only need to sort age. Using comparable can't meet this requirement. Different 'comparators' can be written to meet various sorting requirements.
#1. Write the 'personsortable2' class
Properties: ` private name (string), ` private age (int)`
Parameter constructor: the parameter is name, age
ToString method: return format ` name age`
#2. Write comparator class
1. Write the 'namecomparator' class to sort names in * * ascending order**
2. Write the 'agecomparator' class to sort the ages in * * ascending order**
#3. In the main method
1. Enter n
2. Input n lines of name age, and create n objects to put into the array
3. Sort the array in ascending order according to name and output.
4. On the basis of 3, sort the array in ascending order according to age and output.
5. The last two lines use the following code to output all the interfaces implemented by namecomparator and agecomparator.
```
System.out.println(Arrays.toString(NameComparator.class.getInterfaces()));
System.out.println(Arrays.toString(AgeComparator.class.getInterfaces()));
```
###Input example:
```in
five
zhang 15
zhang 12
wang 14
Wang 17
li 17
```
###Output example:
```out
NameC omparator:sort
Wang-17
li-17
wang-14
zhang-15
zhang-12
AgeC omparator:sort
zhang-12
wang-14
zhang-15
Wang-17
li-17
//The last two lines are identification information
```
answer:If there is no answer, please comment
#1. Write the 'personsortable2' class
Properties: ` private name (string), ` private age (int)`
Parameter constructor: the parameter is name, age
ToString method: return format ` name age`
#2. Write comparator class
1. Write the 'namecomparator' class to sort names in * * ascending order**
2. Write the 'agecomparator' class to sort the ages in * * ascending order**
#3. In the main method
1. Enter n
2. Input n lines of name age, and create n objects to put into the array
3. Sort the array in ascending order according to name and output.
4. On the basis of 3, sort the array in ascending order according to age and output.
5. The last two lines use the following code to output all the interfaces implemented by namecomparator and agecomparator.
```
System.out.println(Arrays.toString(NameComparator.class.getInterfaces()));
System.out.println(Arrays.toString(AgeComparator.class.getInterfaces()));
```
###Input example:
```in
five
zhang 15
zhang 12
wang 14
Wang 17
li 17
```
###Output example:
```out
NameC omparator:sort
Wang-17
li-17
wang-14
zhang-15
zhang-12
AgeC omparator:sort
zhang-12
wang-14
zhang-15
Wang-17
li-17
//The last two lines are identification information
```
answer:If there is no answer, please comment