NormalizedKeySorter implements IndexedSortable { 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); final byte[] bI = segI.getBackingArray(); final byte[] bJ = segJ.getBackingArray(); int val = 0; for (int pos = 0, posI = segI.translateOffset(segmentOffsetI + OFFSET_LEN), posJ = segJ.translateOffset(segmentOffsetJ + OFFSET_LEN); pos < this.numKeyBytes && (val = (bI[posI] & 0xff) - (bJ[posJ] & 0xff)) == 0; pos++, posI++, posJ++); if (val != 0 || this.normalizedKeyFullyDetermines) { return this.useNormKeyUninverted ? val : -val; } final long pointerI = segI.getLong(segmentOffsetI); final long pointerJ = segJ.getLong(segmentOffsetJ); 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); void reset(); boolean isEmpty(); List<MemorySegment> dispose(); long getCapacity(); long getOccupancy(); void getRecord(T target, int logicalPosition); boolean write(T record); int compare(int i, int j); @Override void swap(int i, int j); @Override int size(); final MutableObjectIterator<T> getIterator(); void writeToOutput(final ChannelWriterOutputView output); 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<PactRecord> sorter = newSortBuffer(memory); TestData.Generator generator = new TestData.Generator(SEED, KEY_MAX, VALUE_LENGTH, KeyMode.SORTED, ValueMode.RANDOM_LENGTH); PactRecord record = new PactRecord(); 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); } } this.memoryManager.release(sorter.dispose()); }