How to sort intList every 5 elements

im by inserting thousands of numbers from a txt file into a list, and I want to sort them every 5 numbers. Is this possible, and if so, how can this be done?

     public static void readFromFile(){
     List<Integer> putInList = new ArrayList<Integer>();
     int jNum;
     TextIO.readFile("jokerNums.txt");//read from a spesific file.

     try{
         do{
             jNum = TextIO.getInt();
             putInList.add(jNum);

         } while(!TextIO.eof());//  Test whether the next character in 
         //the current input source is an end-of-file 
     } 

     catch(IllegalArgumentException e){

     } 
     TextIO.put(putInList);
 }

I tried the for loop inside the do {} while loop, but its infinite. Just to mention the numbers I put in, 9785. Thank you in advance.

+4
source share
1 answer
for (int i = 0; !TextIO.eof(); i++){
      int value = TextIO.getInt();
      int targetIndex = (i/5)*5;
      for (; targetIndex < putInList.size(); targetIndex++)
      {
          if(putInList.get(targetIndex)>value)
          {
            break;
          }
      }
      putInList.add(targetIndex,value);

    }
+1
source

Source: https://habr.com/ru/post/1655647/


All Articles