# 排序方式：从高到低排序方式：从低到高Comparator.comparing

##### 问题描述投票：0回答：2

``````String [][]Data={{"Eugine","8"},{"Ben","9"},{"John","19"},{"Jairus","5"},{"Sofia","13"}};
``````

[输出排序：从高到低John“，19索非亚，13 Ben”，9尤金，8 air鲁，5排序方式：低至高air牛，5尤金，8本，9索非亚，13约翰，19

``````//This is just my noob code// //please help//
String [][] Data={{"Eugine","8"},{"Ben","9"},{"John","19"},{"Jairus","5"},{"Sofia","13"}};
Arrays.sort(people);
for(Person p : people)
System.out.println(p.name + ", " + p.age);
}
}

class Person implements Comparable<Person>{
public final String name;
public final int age;[enter image description here][1]
public Person(String name, int age){
this.name = name;
this.age = age;
}
@Override
public int compareTo(Person person) {
return age - person.age;
}
``````
java arrays string methods bubble-sort
##### 2个回答
0

``````import java.util.*;

class Main {
public static void main(String[] args) {

TreeMap<Integer, String> map = new TreeMap<>();

map.put(8, "Eugene");
map.put(9, "Ben");
map.put(19, "John");
map.put(5, "Jairus");
map.put(13, "Sofia");

System.out.println("From low to high");

for(Integer i : map.keySet()){
System.out.println(map.get(i) + ", " + i);
}

TreeMap<Integer, String> revMap =
new TreeMap<Integer, String>(Collections.reverseOrder());

revMap.putAll(map);

System.out.println("\nFrom high to low");
for(Integer i : revMap.keySet()){
System.out.println(revMap.get(i) + ", " + i);
}
}
}
``````

0

# 通过创建单独的比较器

``````import java.util.Arrays;
import java.util.Comparator;

class Person {
private String name;
private int age;

public Person(String name, int age) {
this.name = name;
this.age = age;
}

public String getName() {
return name;
}

public int getAge() {
return age;
}

@Override
public String toString() {
return name + ", " + age;
}
}

class LowToHighComparator implements Comparator<Person> {

@Override
public int compare(Person p1, Person p2) {
return Integer.compare(p1.getAge(), p2.getAge());
}
}

class HighToLowComparator implements Comparator<Person> {

@Override
public int compare(Person p1, Person p2) {
return Integer.compare(p2.getAge(), p1.getAge());
}
}

public class Main {
public static void main(String[] args) {
Person[] people = { new Person("Eugine", 8), new Person("Ben", 9), new Person("John", 19),
new Person("Jairus", 5), new Person("Sofia", 13) };

Person[] lowToHigh = people.clone();
Person[] highToLow = people.clone();

Arrays.sort(lowToHigh, new LowToHighComparator());
Arrays.sort(highToLow, new HighToLowComparator());

System.out.println("Unsorted: ");
Arrays.stream(people).forEach(System.out::println);
System.out.println();

System.out.println("Low to high: ");
Arrays.stream(lowToHigh).forEach(System.out::println);
System.out.println();

System.out.println("High to low: ");
Arrays.stream(highToLow).forEach(System.out::println);
}
}
``````

``````Unsorted:
Eugine, 8
Ben, 9
John, 19
Jairus, 5
Sofia, 13

Low to high:
Jairus, 5
Eugine, 8
Ben, 9
Sofia, 13
John, 19

High to low:
John, 19
Sofia, 13
Ben, 9
Eugine, 8
Jairus, 5
``````

# 通过使用Comparator.comparing

``````import java.util.Arrays;
import java.util.Comparator;

class Person {
private String name;
private int age;

public Person(String name, int age) {
this.name = name;
this.age = age;
}

public String getName() {
return name;
}

public int getAge() {
return age;
}

@Override
public String toString() {
return name + ", " + age;
}
}

public class Main {
public static void main(String[] args) {
Person[] people = { new Person("Eugine", 8), new Person("Ben", 9), new Person("John", 19),
new Person("Jairus", 5), new Person("Sofia", 13) };

Person[] lowToHigh = people.clone();
Person[] highToLow = people.clone();

Arrays.sort(lowToHigh, Comparator.comparing(Person::getAge));
Arrays.sort(highToLow, Comparator.comparing(Person::getAge).reversed());

System.out.println("Unsorted: ");
Arrays.stream(people).forEach(System.out::println);
System.out.println();

System.out.println("Low to high: ");
Arrays.stream(lowToHigh).forEach(System.out::println);
System.out.println();

System.out.println("High to low: ");
Arrays.stream(highToLow).forEach(System.out::println);
}
}
``````

``````Unsorted:
Eugine, 8
Ben, 9
John, 19
Jairus, 5
Sofia, 13

Low to high:
Jairus, 5
Eugine, 8
Ben, 9
Sofia, 13
John, 19

High to low:
John, 19
Sofia, 13
Ben, 9
Eugine, 8
Jairus, 5
``````