如何在Java中对对象数组进行排序
问题描述:
我有一个对象列表与汽车的名称,并在此列表中我有一些车辆对象。所有的对象值都是彼此不同的。在这里的所有对象是自定义字段与单位的名称和单位是另一个字段的名称为unitType ...这是“T20”,“VT11”或任何其他.. 我想排序这辆车根据unitType如: 如果cars(0).getUnit()。getUnitType()。equals(“T20”)先将所有的值先排序然后再将其他的“VT11”排序,然后所有其他... 我该怎么做?这里有什么合适的方法在Java?如何在Java中对对象数组进行排序
我单位类:
package com.vehicletracking.vtss.classes.bean;
import java.io.*;
public class Unit implements Serializable{
private int code;
private int unitId;
private String unitType; //X8, X1, X1+, VT10, VT200, SPT100
private Sim sim;
private String commType; //CSD, SMS, GPRS, AUTO
private int modemCode;
private long IMEI;
private String serialNo;
private InputReportProfile inputReportProfile;
private String firmware;
private String packageName;
private String password;
public Unit() {
}
public Unit(int unitId) {
this.unitId = unitId;
}
public Unit(int unitId, String unitType) {
this.unitId = unitId;
this.unitType = unitType;
}
public Unit(int unitId, String unitType, Sim sim) {
this.unitId = unitId;
this.unitType = unitType;
this.sim = sim;
}
public void setUnitId(int unitId) {
this.unitId = unitId;
}
public int getUnitId() {
return this.unitId;
}
public void setUnitType(String unitType) {
this.unitType = unitType;
}
public String getUnitType() {
return this.unitType;
}
public void setSim(Sim sim) {
this.sim = sim;
}
public void setCommType(String commType) {
this.commType = commType;
}
public void setModemCode(int modemCode) {
this.modemCode = modemCode;
}
public void setSerialNo(String serialNo) {
this.serialNo = serialNo;
}
public void setCode(int code) {
this.code = code;
}
public void setInputReportProfile(InputReportProfile inputReportProfile) {
this.inputReportProfile = inputReportProfile;
}
public void setPassword(String password) {
this.password = password;
}
public void setIMEI(long IMEI) {
this.IMEI = IMEI;
}
public Sim getSim() {
return this.sim;
}
public String getCommType() {
return commType;
}
public int getModemCode() {
return modemCode;
}
public String getSerialNo() {
return serialNo;
}
public int getCode() {
return code;
}
public InputReportProfile getInputReportProfile() {
return inputReportProfile;
}
public String getPassword() {
return password;
}
public long getIMEI() {
return IMEI;
}
public int hashCode() {
return unitId;
}
public boolean equals(Object obj) {
if (obj instanceof Unit) {
return this.unitId == ((Unit) obj).getUnitId();
}
return false;
}
public String toString() {
return this.unitId + "/" + this.unitType;
}
public String getFirmware() {
return firmware;
}
public void setFirmware(String firmware) {
this.firmware= firmware;
}
public String getPackageName() {
return packageName;
}
public void setPackageName(String packageName) {
this.packageName = packageName;
}
}
汽车名单: 列表汽车;
答
从Java 8文档为Comparable:
这个接口规定了实现它的每个类的对象进行整体排序。这种顺序被称为类的自然顺序,类的compareTo方法被称为其自然比较方法。
实现此接口的对象的列表(和数组)可以由Collections.sort(和Arrays.sort)自动排序。实现此接口的对象可用作有序映射中的键或排序集中的元素,而无需指定比较器。
根据上述,您只需实现Comparable,并为您的对象提供compareTo的实现。 Collections.sort负责其余部分。
您可以使用比较器并通过覆盖它来定义自己的排序类型,按最后一个字符串或字符串排序它们。 Collection.sort(whattosor,new Comparator)....... –
如你在问题中所说的那样,在标题或“List”中说的是否有**数组**? (数组不是'List',而'ArrayList'是'List'而不是数组)。 – Jesper
这是重复的。有很多问题询问如何排序。请参见[如何在java中对一组对象进行排序?](http://stackoverflow.com/q/18895915/217324) –