public void sortPostingList(){
        File sort_done_f =new File(indexDirectory+"/sort_done.dat");
        if(!sort_done_f.exists())
            sort_done_f.delete();
        System.err.println("Sorting posting lists, depending on their size and number, it might take a while...");
        try{
            String newDir=indexDirectory+"/new";
            String tempDir=indexDirectory+"/temp";
            String invFileDir=indexDirectory+"/invfile";
            //first let's move the inverted file somewehere else
            moveInvertedFileInTemp(invFileDir,tempDir);
            //let's process the new posting lists
            for(int entry=0;entry<writeInvFile.length;entry++){
                if(((double)(((double)entry*100)/(double)writeInvFile.length)%10)==0)
                    System.err.println(((entry*100)/writeInvFile.length)+"% posting lists processed...");

                File pl_f=new File(newDir+"/pl_e"+entry+".dat");
                File ospl_f=new File(tempDir+"/pl_e"+entry+"_sorted.dat");
                File spl_f=new File(invFileDir+"/pl_e"+entry+"_sorted.dat");

                if(!pl_f.exists()){           //if there is not unordered posting list we just move the old posting list back
                    ospl_f.renameTo(spl_f);
                }
                else  {
                    RandomAccessFile pl_ra=new RandomAccessFile(pl_f,"r");
                    RandomAccessFile ospl_ra=null;

                    int[] scoreCounter=new int[writeInvFile.length];            //array maintaining for each possible score the number of objects having that score
                    int[] objectsWithScore=new int[writeInvFile.length];          //array indicating for each possible score what is the current number of objects inserted in the ordered posting list being created
                    int sizeOfPostingListIndex=(scoreCounter.length+1)*4;       //size of the persistent array used as an index in the posting list to access objects having a certain score
                    //We first count how many objects have a certain score in the old posting lists and we also get the size of the old posting list
                    long sizeOfOldPostingList=0;
                    if(ospl_f.exists()){
                        ospl_ra=new RandomAccessFile(ospl_f,"rw");
                        sizeOfOldPostingList=countObjectsWithScoreInOldPostingLists(ospl_ra,scoreCounter, objectsWithScore, sizeOfPostingListIndex);
                    }
                    //We add the number of new objects that have a certain score in the new unordered posting list
                    MappedByteBuffer pl=pl_ra.getChannel().map(java.nio.channels.FileChannel.MapMode.READ_ONLY,0,pl_ra.length());
                    countObjectsWithScoreInUnsortedPostingLists(pl, scoreCounter);
                    //let's create a new ordered posting list  
                    RandomAccessFile spl_ra=new RandomAccessFile(spl_f,"rw");   //We create a new ordered posting list
                    MappedByteBuffer spl=spl_ra.getChannel().map(java.nio.channels.FileChannel.MapMode.READ_WRITE,0,sizeOfOldPostingList+pl_ra.length()/2+sizeOfPostingListIndex);  //given that we do not store the score, //The length of the new posting list should be the old ordered one + half of the unordered one
                    //if an ordered posting list already exists its content should be copied in the new created odered posting list
                    if(ospl_f.exists()){
                        copyOldIntoNewPostingList(ospl_ra, spl, scoreCounter, objectsWithScore, sizeOfPostingListIndex);
                    }
                    //Now we can re-read the unordered posting list and copy the element in the proper sector of the ordered posting list
                    copyUnsortedIntoNewPostingList(pl, spl, scoreCounter, objectsWithScore, sizeOfPostingListIndex);
                    //We update the index of the ordered posting list
                    updatePostingListIndex(spl,scoreCounter, sizeOfPostingListIndex);
                    //close the new sorted posting list
                    closeOpenFiles(pl,  spl,  pl_ra,  spl_ra);
                }
            }
            //deleting old and temporary files
            cleanUpOldAndTemporaryfiles( newDir,  tempDir);
            
            sort_done_f.createNewFile();
        }catch(Exception e){
                System.out.println("Cannot sort posting list");
                e.printStackTrace();
            }
        System.err.println("Sorting posting lists finished!");
    }