5

I have two lists which holds two different types of objects.

  1. Protocol(obj1)
  2. Sensor(obj2)

Protocol list is sorted based on a property(protocolId) using "Collections.sort" using comparator. Now both Protocol and Sensor objects has 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 property "referenceName".

Since the Comparator can only compare two objects with in the same list. I am facing problems with comparing the first list with the second. Could somebody help me out?

6
  • can you mix both in a temporal list and sort that? Commented May 16, 2017 at 7:01
  • is referenceName unique? Commented May 16, 2017 at 7:02
  • @BillF yes, it is unique Commented May 16, 2017 at 7:03
  • what do you mean by " in the same order of the Protocol list using the property "referenceName". " Commented May 16, 2017 at 7:04
  • it's unclear what you are asking, could you provide a sample how it should work? why can't you sort the second array by the referenceName? Commented May 16, 2017 at 7:05

5 Answers 5

4

Why don't you have a new class that holds both Protocol and Sensor with particular referenceName and sort that. Following is the pseudo-code of the class -

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
  }
}
Sign up to request clarification or add additional context in comments.

5 Comments

this makes sense ! :)
How can this class be used to sort a Sensor array?
This groups Protocol and Sensor, so when Protocols get sorted, the Sensors also get sorted automatically.
Right, but as I understood the requirement is to have a Protocol collection sorted by protocolId, and have Sensor collection sorted the same way by referenceName as Protocol was sorted. How will the ProtocolSensorGroup class support this bi-sorted structure?
Right, then they should change String refNameLhs = lhs.getReferenceName(); lines to String refNameLhs = lhs.getProtocol().getId();
2

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

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 Comment

It Worked. Thanks a lot!
0

What you can do is create list of indexes

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

now sort this list using a comparator where the compareTo method will compare not the 2 integers, it will compare the equivelent objects in the ProtocolList

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

now you have your indexes list ordered, you can refill both the lists you have using it by using the ordered indexes, for example for Sensor List

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
 }    

Comments

0

I see two options: 1. Have Protocol(obj1) as a class level variable, so you can access it in the Comparator without passing it as a parameter. This will not work if you have several Protocol structures. 2. Implement your own sorting method: loop through Protocol(obj1), and find the corresponding Sensor(obj2). This can be improved in performance if you pre-sort Sensor(obj) by referenceName, and implement a binary search instead of a full loop.

Comments

0

Pass your list to Comparator through constructor ,then use the list in Your logic

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

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.