ArrayList sorted by entity class

package com.yu.entity;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
//Inherit the Comparable interface
public class SocketEntity implements Comparable<SocketEntity> {

	public static final int SEND_DATA = 1; //Send data
	public static final int GET_DATA = 2; //Get data
	public static final int DELETE_DATA = 3; //delete data
	public static final int DATA_BUFF = 1024; //Write to read buffer
	
	public static final int FREE = 1; //client idle
	public static final int BUSY = 0; //client is busy
	
	private long frequency = 0L; //Use frequency
	
	private String name = null; //client name
	
	private Socket socket = null; //client

	private int status = 0; //Service status idle FREE busy BUSY
	
	OutputStream os = null; //The output stream writes data to the client
	
	InputStream is = null; //The input stream reads data to the client
	
	public SocketEntity(String name, Socket socket, int status) {
		this.name = name;
		this.socket = socket;
		this.status = status;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public Socket getSocket() {
		return socket;
	}

	public void setSocket(Socket socket) {
		this.socket = socket;
	}

	public int getStatus() {
		return status;
	}

	public void setStatus(int status) {
		this.status = status;
	}

	public long getFrequency() {
		return frequency;
	}

	public void setFrequency(long frequency) {
		this.frequency = frequency;
	}

	public void frequencyAddition(){
		long frequency = this.getFrequency();
		this.setFrequency(frequency++);
	}
	
	public OutputStream getOs() {
		try {
			return this.getSocket().getOutputStream();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace ();
		}
		return null;
	}

	public InputStream getIs() {
		try {
			return this.getSocket().getInputStream();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace ();
		}
		return null;
	}

	@Override
	public int compareTo(SocketEntity o) {
		
		if (this.getFrequency() == o.getFrequency()){
			return 0; //equal to
		}else if (this.getFrequency() > o.getFrequency()){
			return -1; //less than
		}else{
			return 1; // greater than
		}
	}
	
}

 The calling method is as follows:

Collections.sort(socketEntity);

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326138402&siteId=291194637