NormalizedKeySorter implements InMemorySorter<T> { @Override public int compare(int i, int j) { final int bufferNumI = i / this.indexEntriesPerSegment; final int segmentOffsetI = (i % this.indexEntriesPerSegment) * this.indexEntrySize; final int bufferNumJ = j / this.indexEntriesPerSegment; final int segmentOffsetJ = (j % this.indexEntriesPerSegment) * this.indexEntrySize; final MemorySegment segI = this.sortIndex.get(bufferNumI); final MemorySegment segJ = this.sortIndex.get(bufferNumJ); int val = segI.compare(segJ, segmentOffsetI + OFFSET_LEN, segmentOffsetJ + OFFSET_LEN, this.numKeyBytes); if (val != 0 || this.normalizedKeyFullyDetermines) { return this.useNormKeyUninverted ? val : -val; } final long pointerI = segI.getLong(segmentOffsetI) & POINTER_MASK; final long pointerJ = segJ.getLong(segmentOffsetJ) & POINTER_MASK; return compareRecords(pointerI, pointerJ); } NormalizedKeySorter(TypeSerializer<T> serializer, TypeComparator<T> comparator, List<MemorySegment> memory);  NormalizedKeySorter(TypeSerializer<T> serializer, TypeComparator<T> comparator, 
			List<MemorySegment> memory, int maxNormalizedKeyBytes); @Override void reset(); @Override boolean isEmpty(); @Override void dispose(); @Override long getCapacity(); @Override long getOccupancy(); @Override T getRecord(int logicalPosition); @Override T getRecord(T reuse, int logicalPosition); @Override boolean write(T record); @Override int compare(int i, int j); @Override void swap(int i, int j); @Override int size(); @Override final MutableObjectIterator<T> getIterator(); @Override void writeToOutput(ChannelWriterOutputView output); @Override void writeToOutput(ChannelWriterOutputView output, LargeRecordHandler<T> largeRecordsOutput); @Override void writeToOutput(final ChannelWriterOutputView output, final int start, int num);  }
@Test public void testCompare() throws Exception { final int numSegments = MEMORY_SIZE / MEMORY_PAGE_SIZE; final List<MemorySegment> memory = this.memoryManager.allocatePages(new DummyInvokable(), numSegments); NormalizedKeySorter<Tuple2<Integer, String>> sorter = newSortBuffer(memory); TestData.TupleGenerator generator = new TestData.TupleGenerator(SEED, KEY_MAX, VALUE_LENGTH, KeyMode.SORTED, ValueMode.RANDOM_LENGTH); Tuple2<Integer, String> record = new Tuple2<>(); int num = -1; do { generator.next(record); num++; } while (sorter.write(record)); Random rnd = new Random(SEED << 1); for (int i = 0; i < 2 * num; i++) { int pos1 = rnd.nextInt(num); int pos2 = rnd.nextInt(num); int cmp = sorter.compare(pos1, pos2); if (pos1 < pos2) { Assert.assertTrue(cmp <= 0); } else { Assert.assertTrue(cmp >= 0); } } sorter.dispose(); this.memoryManager.release(memory); }