使用可比较或比较器对对象进行降序排序
Descending sort of object using comparable or comparator
所以我有一个 Hill 对象,其中包含从 CSV 文件中获取的有关山丘的信息。该文件如下所示:
import java.io.IOException;
import java.net.URL;
import java.util.*;
public class Hill implements Comparable<Hill>{
public static final String CSV_FILE_URL = "HIDDEN";
public static final String DELIMITER = ",";
public int number;
public String name,county;
public double height,lat,lon;
public Hill(int number,String name,String county,double height,double lat,double lon){
this.number = number;
this.name = name;
this.county = county;
this.height = height;
this.lat = lat;
this.lon = lon;
}
public String toString(){
return(number + ", " + name + ", " + county + ", " + height + ", " + lat + ", " + lon);
}
public static List<Hill> readHills() throws IOException{
String[] fields = new String[0];
URL url = new URL(CSV_FILE_URL);
Scanner input = new Scanner(url.openConnection().getInputStream());
List<Hill> hillList = new ArrayList<>();
input.nextLine();
while(input.hasNextLine()){
fields = input.nextLine().split(DELIMITER);
Hill hill = new Hill(Integer.parseInt(fields[0]),fields[1],fields[2],Double.parseDouble(fields[3]),Double.parseDouble(fields[4]),Double.parseDouble(fields[5]));
hillList.add(hill);
}
return hillList;
}
@Override
public int compareTo(Hill o) {
return this.name.compareTo(o.name);
}
}
在我的练习中 class 我目前能够使用 Comparable 界面按字母顺序对山丘进行排序。然后我让它打印前 20 个山丘。
现在我希望按高度降序排列山丘列表,以便我可以打印出 20 个最高的山丘。
import java.io.IOException;
import java.util.*;
public class Exercise5 {
public static void exercise5d() throws IOException {
System.out.println("### Exercise 5D ###");
List listOfHills = Hill.readHills();
Collections.sort(listOfHills);
for(int x=0;x<20;x++){
System.out.println(listOfHills.get(x));
}
System.out.println("");
//Attempt at reversing
Comparator<Hill> HillComparator = Collections.reverseOrder();
Collections.sort(listOfHills,HillComparator);
for(int x=0;x<20;x++){
System.out.println(listOfHills.get(x));
}
}
public static void main(String[] args) throws IOException {
exercise5d();
}
}
这是一个示例,说明如何使用 Comparable
或 Comparator
:
/*
** Use the Collections API to sort a List for you.
**
** When your class has a "natural" sort order you can implement
** the Comparable interface.
**
** You can use an alternate sort order when you implement
** a Comparator for your class.
*/
import java.util.*;
public class Person implements Comparable<Person>
{
String name;
int age;
public Person(String name, int age)
{
this.name = name;
this.age = age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public String toString()
{
return name + " : " + age;
}
/*
** Implement the natural order for this class
*/
public int compareTo(Person p)
{
return getName().compareTo(p.getName());
}
static class AgeComparator implements Comparator<Person>
{
public int compare(Person p1, Person p2)
{
return p1.getAge() - p2.getAge();
}
}
public static void main(String[] args)
{
List<Person> people = new ArrayList<Person>();
people.add( new Person("Homer", 38) );
people.add( new Person("Marge", 35) );
people.add( new Person("Bart", 15) );
people.add( new Person("Lisa", 13) );
// Sort by natural order
Collections.sort(people);
System.out.println("Sort by Natural order");
System.out.println("\t" + people);
// Sort by reverse natural order
Collections.sort(people, Collections.reverseOrder());
System.out.println("Sort by reverse natural order");
System.out.println("\t" + people);
// Use a Comparator to sort by age
Collections.sort(people, new Person.AgeComparator());
System.out.println("Sort using Age Comparator");
System.out.println("\t" + people);
// Use a Comparator to sort by descending age
Collections.sort(people, Collections.reverseOrder(new Person.AgeComparator()));
System.out.println("Sort using Reverse Age Comparator");
System.out.println("\t" + people);
// Use a Comparator with lambda expression to sort by age
// Collections.sort(people, (o1, o2) -> o1.getAge() - o2.getAge());
// Collections.sort(people, Comparator.comparingInt(p -> p.getAge()));
Collections.sort(people, Comparator.comparingInt(Person::getAge));
System.out.println("Sort using Lambda Age Comparator");
System.out.println("\t" + people);
}
}
所以你只需要为你的 Hill
class 在你想要的 属性(s) 上实现 Comparator
。
你的基本实现Comparable
比较的是山的名字,与高度无关
How can I adjust Comparable so that it allows me to sort by name and then by height after?
创建一个新的 Comparator 来实现您需要比较高度的逻辑并将其与 Collections.sort
一起使用
Collections.sort(listOfHills, new Comparator<Hill>() {
@Override
public int compare(Hill o1, Hill o2) {
return o2.height > o1.height ? (o2.height == o1.height ? 0 : 1) : -1;
}
});
这是我通常不在对象上实现 Comparator
的原因之一,除非它们是与对象比较方式相关的良好业务规则,否则很容易提供一些自定义 Comparator
s(或允许其他开发人员设计自己的)实现 "common" 算法 - 但那是我
所以我有一个 Hill 对象,其中包含从 CSV 文件中获取的有关山丘的信息。该文件如下所示:
import java.io.IOException;
import java.net.URL;
import java.util.*;
public class Hill implements Comparable<Hill>{
public static final String CSV_FILE_URL = "HIDDEN";
public static final String DELIMITER = ",";
public int number;
public String name,county;
public double height,lat,lon;
public Hill(int number,String name,String county,double height,double lat,double lon){
this.number = number;
this.name = name;
this.county = county;
this.height = height;
this.lat = lat;
this.lon = lon;
}
public String toString(){
return(number + ", " + name + ", " + county + ", " + height + ", " + lat + ", " + lon);
}
public static List<Hill> readHills() throws IOException{
String[] fields = new String[0];
URL url = new URL(CSV_FILE_URL);
Scanner input = new Scanner(url.openConnection().getInputStream());
List<Hill> hillList = new ArrayList<>();
input.nextLine();
while(input.hasNextLine()){
fields = input.nextLine().split(DELIMITER);
Hill hill = new Hill(Integer.parseInt(fields[0]),fields[1],fields[2],Double.parseDouble(fields[3]),Double.parseDouble(fields[4]),Double.parseDouble(fields[5]));
hillList.add(hill);
}
return hillList;
}
@Override
public int compareTo(Hill o) {
return this.name.compareTo(o.name);
}
}
在我的练习中 class 我目前能够使用 Comparable 界面按字母顺序对山丘进行排序。然后我让它打印前 20 个山丘。
现在我希望按高度降序排列山丘列表,以便我可以打印出 20 个最高的山丘。
import java.io.IOException;
import java.util.*;
public class Exercise5 {
public static void exercise5d() throws IOException {
System.out.println("### Exercise 5D ###");
List listOfHills = Hill.readHills();
Collections.sort(listOfHills);
for(int x=0;x<20;x++){
System.out.println(listOfHills.get(x));
}
System.out.println("");
//Attempt at reversing
Comparator<Hill> HillComparator = Collections.reverseOrder();
Collections.sort(listOfHills,HillComparator);
for(int x=0;x<20;x++){
System.out.println(listOfHills.get(x));
}
}
public static void main(String[] args) throws IOException {
exercise5d();
}
}
这是一个示例,说明如何使用 Comparable
或 Comparator
:
/*
** Use the Collections API to sort a List for you.
**
** When your class has a "natural" sort order you can implement
** the Comparable interface.
**
** You can use an alternate sort order when you implement
** a Comparator for your class.
*/
import java.util.*;
public class Person implements Comparable<Person>
{
String name;
int age;
public Person(String name, int age)
{
this.name = name;
this.age = age;
}
public String getName()
{
return name;
}
public int getAge()
{
return age;
}
public String toString()
{
return name + " : " + age;
}
/*
** Implement the natural order for this class
*/
public int compareTo(Person p)
{
return getName().compareTo(p.getName());
}
static class AgeComparator implements Comparator<Person>
{
public int compare(Person p1, Person p2)
{
return p1.getAge() - p2.getAge();
}
}
public static void main(String[] args)
{
List<Person> people = new ArrayList<Person>();
people.add( new Person("Homer", 38) );
people.add( new Person("Marge", 35) );
people.add( new Person("Bart", 15) );
people.add( new Person("Lisa", 13) );
// Sort by natural order
Collections.sort(people);
System.out.println("Sort by Natural order");
System.out.println("\t" + people);
// Sort by reverse natural order
Collections.sort(people, Collections.reverseOrder());
System.out.println("Sort by reverse natural order");
System.out.println("\t" + people);
// Use a Comparator to sort by age
Collections.sort(people, new Person.AgeComparator());
System.out.println("Sort using Age Comparator");
System.out.println("\t" + people);
// Use a Comparator to sort by descending age
Collections.sort(people, Collections.reverseOrder(new Person.AgeComparator()));
System.out.println("Sort using Reverse Age Comparator");
System.out.println("\t" + people);
// Use a Comparator with lambda expression to sort by age
// Collections.sort(people, (o1, o2) -> o1.getAge() - o2.getAge());
// Collections.sort(people, Comparator.comparingInt(p -> p.getAge()));
Collections.sort(people, Comparator.comparingInt(Person::getAge));
System.out.println("Sort using Lambda Age Comparator");
System.out.println("\t" + people);
}
}
所以你只需要为你的 Hill
class 在你想要的 属性(s) 上实现 Comparator
。
你的基本实现Comparable
比较的是山的名字,与高度无关
How can I adjust Comparable so that it allows me to sort by name and then by height after?
创建一个新的 Comparator 来实现您需要比较高度的逻辑并将其与 Collections.sort
一起使用Collections.sort(listOfHills, new Comparator<Hill>() {
@Override
public int compare(Hill o1, Hill o2) {
return o2.height > o1.height ? (o2.height == o1.height ? 0 : 1) : -1;
}
});
这是我通常不在对象上实现 Comparator
的原因之一,除非它们是与对象比较方式相关的良好业务规则,否则很容易提供一些自定义 Comparator
s(或允许其他开发人员设计自己的)实现 "common" 算法 - 但那是我