Java 怎么用compareTo接口实现排序
我有一个Book类,代码如下:publicclassBook{privateStringauthorfirstname;privateStringtitle;privat...
我有一个Book类,代码如下:
public class Book
{
private String authorfirstname;
private String title;
private String borrowedperson;
private boolean loan;
private String authorsurname;
public Book(String title,String authorfirstname, String authorsurname, String borrowedperson, boolean loan)
{
this.title = title;
this.authorfirstname = authorfirstname;
this.authorsurname = authorsurname;
this.borrowedperson = borrowedperson;
this.loan = loan;
}
public String getauthorfirstname()
{
return authorfirstname;
}
public String gettitle()
{
return title;
}
public String getborrowedperson()
{
return this.borrowedperson;
}
public boolean getloan()
{
return loan;
}
然后再另一个类中定义了一个数组a,储存了好多Book类型的元素。
现在要求用compareTo接口对Book类中的authorsurname进行升序排序,求大神告知如何排啊 展开
public class Book
{
private String authorfirstname;
private String title;
private String borrowedperson;
private boolean loan;
private String authorsurname;
public Book(String title,String authorfirstname, String authorsurname, String borrowedperson, boolean loan)
{
this.title = title;
this.authorfirstname = authorfirstname;
this.authorsurname = authorsurname;
this.borrowedperson = borrowedperson;
this.loan = loan;
}
public String getauthorfirstname()
{
return authorfirstname;
}
public String gettitle()
{
return title;
}
public String getborrowedperson()
{
return this.borrowedperson;
}
public boolean getloan()
{
return loan;
}
然后再另一个类中定义了一个数组a,储存了好多Book类型的元素。
现在要求用compareTo接口对Book类中的authorsurname进行升序排序,求大神告知如何排啊 展开
3个回答
展开全部
package test;
public class Field implements Comparable<Field> {
private String name;
private int age;
public Field() {
}
public Field(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public int compareTo(Field o) {
// 先按age排序
if (this.age > o.getAge()) {
return (this.age - o.getAge());
}
if (this.age < o.getAge()) {
return (this.age - o.getAge());
}
// 按name排序
if (this.name.compareTo(o.getName()) > 0) {
return 1;
}
if (this.name.compareTo(o.getName()) < 0) {
return -1;
}
return 0;
}
}
展开全部
public class Book implements Comparable
{
private String authorfirstname;
private String title;
private String borrowedperson;
private boolean loan;
private String authorsurname;
public Book(String title,String authorfirstname, String authorsurname, String borrowedperson, boolean loan)
{
this.title = title;
this.authorfirstname = authorfirstname;
this.authorsurname = authorsurname;
this.borrowedperson = borrowedperson;
this.loan = loan;
}
public String getauthorfirstname()
{
return authorfirstname;
}
public String gettitle()
{
return title;
}
public String getborrowedperson()
{
return this.borrowedperson;
}
public boolean getloan()
{
return loan;
}
@Override
public int compareTo(Object o)
{
// 自己写条件
return 0;
}
}
希望采纳
{
private String authorfirstname;
private String title;
private String borrowedperson;
private boolean loan;
private String authorsurname;
public Book(String title,String authorfirstname, String authorsurname, String borrowedperson, boolean loan)
{
this.title = title;
this.authorfirstname = authorfirstname;
this.authorsurname = authorsurname;
this.borrowedperson = borrowedperson;
this.loan = loan;
}
public String getauthorfirstname()
{
return authorfirstname;
}
public String gettitle()
{
return title;
}
public String getborrowedperson()
{
return this.borrowedperson;
}
public boolean getloan()
{
return loan;
}
@Override
public int compareTo(Object o)
{
// 自己写条件
return 0;
}
}
希望采纳
追问
条件怎么写,我刚开始学不太会,这个类我只要求对authorsurname一个量进行排序
追答
那就判断这个条件啊
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询