2

I have an object List with the name of cars and in this list I have some vehicle object. all objects values are different to each other. In all object here is custom field with the name of unit and in unit is another field with the name of unitType ... which is "T20" , "VT11" or any other .. I want to sort this cars List according to unitType like: if cars(0).getUnit().getUnitType().equals("T20") sort all the value first then other "VT11" and then all other... How can I do this?? Is any suitable method in java?

my Unit class:

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;
    }


}

Car List: List cars;

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
zeeshan nazakat
  • 151
  • 3
  • 18
  • 3
    you can use the Comparator and define your own sorting type by overriding it, where to sort them by last inter or the string. Collection.sort(whattosor, new Comparator)....... – Seek Addo Jun 28 '16 at 11:59
  • 1
    Do you have an **array** as you say in the title or a `List` as you say in the question? (An array is not a `List` and an `ArrayList` is a `List` and not an array). – Jesper Jun 28 '16 at 12:00
  • This is a duplicate. there are lots of questions asking about how to sort. See [How to sort an array of objects in java?](http://stackoverflow.com/q/18895915/217324) – Nathan Hughes Jun 28 '16 at 12:04

1 Answers1

1

From Java 8 Documentation for Comparable:

This interface imposes a total ordering on the objects of each class that implements it. This ordering is referred to as the class's natural ordering, and the class's compareTo method is referred to as its natural comparison method.

Lists (and arrays) of objects that implement this interface can be sorted automatically by Collections.sort (and Arrays.sort). Objects that implement this interface can be used as keys in a sorted map or as elements in a sorted set, without the need to specify a comparator.

According to above, you just have to implement Comparable, and provide an implementation for compareTo for your object. Collections.sort takes care of the rest.

Community
  • 1
  • 1
navid
  • 566
  • 6
  • 15