How to sort a list of objects with a property of another object in Java?

I have two lists that contain two different types of objects.

  • Protocol (obj1)
  • Sensor (obj2)

The protocol list is sorted based on the (protocolId) property using the "Collections.sort" command using a comparator. Now both Protocol and Sensor objects have the same property called "referenceName". Since the first list (protocol) is sorted, I want the second list (Sensor) to be sorted in the same order of the protocol list using the "referenceName" property.

Since Comparator can only compare two objects in one list. I ran into problems comparing the first list with the second. Can anyone help me out?

+4
source share
5 answers

You can use Java 8 lambdas to create a mapping between referenceNameand its index in a sorted list Protocol:

public class Protocol {
    final String referenceName;

    public String getReferenceName() {
        return referenceName;
    }

    // other stuff
}

public class Sensor {
    final String referenceName;

    public String getReferenceName() {
        return referenceName;
    }

    // other stuff
}

final List<Protocol> protocols = getProtocols();
final List<Sensor> sensors = getSensors();

// TODO : sort protocols here

// create a mapping between a referenceName and its index in protocols
final Map<String, Integer> referenceNameIndexesMap = IntStream.range(0, protocols.size()).mapToObj(i -> i)
            .collect(Collectors.toMap(i -> protocols.get(i).getReferenceName(), i -> i));

// sort sensors using this mapping
sensors.sort(Comparator.comparing(s -> referenceNameIndexesMap.get(s.getReferenceName())));

// sensors is now sorted in the same order of referenceName as protocols
+1
source

Why don't you have a new class that contains both the protocol and the sensor with a specific reference name and sorts it. Below is the class pseudo code -

class ProtocolSensorGroup implements Comparable {
  private String referenceName;
  private Protocol protocol;
  private Sensor sensor;

  public boolean compareTo(ProtocolSensorGroup lhs, ProtocolSensorGroup rhs) {
    String pidLhs = lhs.getProtocol().getId();
    String pidRhs = rhs.getProtocol().getId();

    return pidLhs.compareTo(pidRhs); // Or your logic here
  }
}
+3
source

,

{0, 1, 3 ...} //0 reprssents the first object in both lists and so on

, compareTo 2 , ProtocolList

   @Override
   public int compareTo(Integer o1, Integer o2) {
      //now compare protocolList.get(o1).getProtocolId, protocolList implementation should not be linkedList
   }

, , , , ,

List<Sensor> orderedSensorList = new ArrayList<>();
for(int i=0; i< indexesList.size(); i++) {//make sure the implementation of the indexes list is not linked list or the time complexity will be O(n^2)
    orderedSensorList.add(sensorList.get(idexesList.get(i));//sensorList is the unordered sensor list
 }    
0

: 1. (obj1) , , . , . 2. : (obj1) (obj2). , Sensor (obj) referenceName .

0

Comparator ,

public class MyComparator implements Comparator  {

        private List<Protocol> protocoList;

        public MyComparator(List<Protocol> protocoList ){
            this.protocoList=protocoList;
        }

        @Override
        public int compare(Object o1, Object o2) {
            // Write logic using reference of list pass through constructor.
            return 0;
        }

    }
0

Source: https://habr.com/ru/post/1677223/


All Articles