@Override
    protected void process(ComplexEventChunk<StreamEvent> streamEventChunk, Processor nextProcessor,
                           StreamEventCloner streamEventCloner, WindowState state) {


        synchronized (state) {
            long currentTime = siddhiQueryContext.getSiddhiAppContext().getTimestampGenerator().currentTime();

            StreamEvent streamEvent = streamEventChunk.getFirst();
            streamEventChunk.clear();
            while (streamEvent != null) {
                StreamEvent clonedEvent = streamEventCloner.copyStreamEvent(streamEvent);
                clonedEvent.setType(StreamEvent.Type.EXPIRED);

                StreamEvent next = streamEvent.getNext();
                streamEvent.setNext(null);
                streamEventChunk.add(streamEvent);

                state.sortedWindow.add(clonedEvent);
                if (state.sortedWindow.size() > lengthToKeep) {
                    Collections.sort(state.sortedWindow, eventComparator);
                    StreamEvent expiredEvent = state.sortedWindow.remove(state.sortedWindow.size() - 1);
                    expiredEvent.setTimestamp(currentTime);
                    streamEventChunk.add(expiredEvent);
                }

                streamEvent = next;
            }
        }
        nextProcessor.process(streamEventChunk);
    }
Sample Query:
from inputStream#window.sort(5, attribute1, "asc", attribute2, "desc")
select attribute1, attribute2
insert into outputStream;
<p>
Description:
In the example query given, 5 is the size of the window.
The arguments following the size of the window are optional.
If neither "asc" nor "desc" is given for a certain attribute, order defaults to "asc"
modules/siddhi-core/src/main/java/io/siddhi/core/query/processor/stream/window/SortWindowProcessor.java