
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;

public class CallAggregator {

    private static class CallPart {
        private long ts;
        private boolean isStart;

        public CallPart(long ts, boolean isStart) {
            this.ts = ts;
            this.isStart = isStart;
        }
    }

    private static class CallPartComparator implements Comparator<CallPart> {
        @Override
        public int compare(CallPart o1, CallPart o2) {
            if (o1.ts == o2.ts) {
                return 0;
            }
            return o1.ts > o2.ts ? 1 : -1;
        }
    }

    public static int max(List<Call> calls) {
        assert (calls != null);

        final List<CallPart> startAndEnds = new LinkedList<>();
        calls.forEach(call -> {
            startAndEnds.add(new CallPart(call.getStartTs(), true));
            startAndEnds.add(new CallPart(call.getEndTs(), false));
        });

        startAndEnds.sort(new CallPartComparator());

        int max = 0;
        int currentNumberOfCall = 0;
        long lastTs = -1;
        for (CallPart callPart : startAndEnds) {
            if (lastTs != callPart.ts) {
                max = Math.max(max, currentNumberOfCall);
            }
            if (callPart.isStart) {
                currentNumberOfCall++;
            } else {
                currentNumberOfCall--;
            }
        }
        max = Math.max(max, currentNumberOfCall);

        return max;
    }
}

@@ -541,18 +541,73 @@
  return 
-0;
+o1.isStart ? 1 : -1; // the ends are before the starts !!
 %0A       
@@ -1114,34 +1114,8 @@
  0;%0A
-        long lastTs = -1;%0A
     
@@ -1163,121 +1163,8 @@
 ) %7B%0A
-            if (lastTs != callPart.ts) %7B%0A                max = Math.max(max, currentNumberOfCall);%0A            %7D%0A
     
@@ -1308,30 +1308,24 @@
       %7D%0A    
-    %7D%0A
         max 
@@ -1357,24 +1357,68 @@
 mberOfCall);
+ // because ends are before starts%0A        %7D
 %0A%0A        re

Simplify the code: while sorting the ends are placed before the starts