使用Collections.sort进行排序
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class SetSortTools {
public static List<BluetoothDevice> getListBluetoothDevice(){
List<BluetoothDevice> bluetoothDeviceList=new ArrayList<>();
bluetoothDeviceList.add(getBluetoothDevice("蓝牙1","xxx-xx-xx-xx-x-1",13));
bluetoothDeviceList.add(getBluetoothDevice("蓝牙2","xxx-xx-xx-xx-x-1",14));
bluetoothDeviceList.add(getBluetoothDevice("蓝牙7","xxx-xx-xx-xx-x-1",20));
bluetoothDeviceList.add(getBluetoothDevice("蓝牙3","xxx-xx-xx-xx-x-1",16));
bluetoothDeviceList.add(getBluetoothDevice("蓝牙4","xxx-xx-xx-xx-x-1",17));
bluetoothDeviceList.add(getBluetoothDevice("蓝牙5","xxx-xx-xx-xx-x-1",17));
bluetoothDeviceList.add(getBluetoothDevice("蓝牙6","xxx-xx-xx-xx-x-1",18));
return bluetoothDeviceList;
}
private static BluetoothDevice getBluetoothDevice(String name,String address,int strength){
BluetoothDevice mBluetoothDevice=new BluetoothDevice();
mBluetoothDevice.setName(name);
mBluetoothDevice.setAddress(address);
mBluetoothDevice.setStrength(strength);
return mBluetoothDevice;
}
public static List<BluetoothDevice> sortBluetoothList(List<BluetoothDevice> bluetoothDeviceList){
Collections.sort(bluetoothDeviceList, new Comparator<BluetoothDevice>() {
@Override
public int compare(BluetoothDevice o1, BluetoothDevice o2) {
return o2.getStrength()-o1.getStrength();
}
});
for (int i = 0; i < bluetoothDeviceList.size(); i++) {
System.out.println(bluetoothDeviceList.get(i).getName()+"===="+bluetoothDeviceList.get(i).getStrength()+"");
}
return bluetoothDeviceList;
}
}
调用
assertEquals("蓝牙7", SetSortTools.sortBluetoothList(SetSortTools.getListBluetoothDevice()).get(0).getName())
|