Stephan Müeller was overjoyed to hear that his coworker, The Specialist, had finally left the company. His coworker earned that title because he had the strange power to turn anything he touched into an unmaintainable, barely-compilable heap of codefiles: methods would swell to thousands of lines of code, case statements would branch to over ten levels, and so on. While browsing through his inventions, Stephan discovered the Quadrasort: it would sort a list of filenames formatted like "YYYYMMDDHH.ext". Sure, using the built-in Collections.Sort() method would do the trick; the format is, after all, designed for lexicographical ordering. But you have to admit, The Specialist's Quadrasort is that much more ... special ....

 

public void sortFiles(Vector filesVector) {
    int len = filesVector.size();
    int i;
    String filesArray[] = new String[len];
    for (i = 0; i < len; i++)
      filesArray[i] = (String) filesVector.get(i);
    filesVector.clear();
    len = filesArray.length - 1;
    String valA, valB;
    int countA, countB, countYearA, countYearB, countMonthA, countMonthB, countDayA, countDayB;
    while (len > 0) {
      for (i = 0; i < len; i++) {
        valA = filesArray[i];
        valB = filesArray[i + 1];
        try {
          countA = Integer.parseInt(valA.substring(0, 4));
          countB = Integer.parseInt(valB.substring(0, 4));
          if (countA <= countB)
            continue;
          filesArray[i] = valB;
          filesArray[i + 1] = valA;
        } catch (NumberFormatException nfe) {
          Log.log( ERROR1+nfe.getMessage() , NPSLOGGER );
        }
      }
      len--;
    }
    len = filesArray.length - 1;
    while (len > 0) {
      for (i = 0; i < len; i++) {
        valA = filesArray[i];
        valB = filesArray[i + 1];
        try {
          countYearA = Integer.parseInt(valA.substring(0, 4));
          countYearB = Integer.parseInt(valB.substring(0, 4));
          countA = Integer.parseInt(valA.substring(4, 6));
          countB = Integer.parseInt(valB.substring(4, 6));
          if (countYearA < countYearB)
            continue;
          if (countA <= countB)
            continue;
          filesArray[i] = valB;
          filesArray[i + 1] = valA;
        } catch (NumberFormatException nfe) {
          Log.log( ERROR1+nfe.getMessage() , NPSLOGGER );
        }
      }
      len--;
    }
    len = filesArray.length - 1;
    while (len > 0) {
      for (i = 0; i < len; i++) {
        valA = filesArray[i];
        valB = filesArray[i + 1];
        try {
          countYearA = Integer.parseInt(valA.substring(0, 4));
          countYearB = Integer.parseInt(valB.substring(0, 4));
          countMonthA = Integer.parseInt(valA.substring(4, 6));
          countMonthB = Integer.parseInt(valB.substring(4, 6));
          countA = Integer.parseInt(valA.substring(6, 8));
          countB = Integer.parseInt(valB.substring(6, 8));
          if (countYearA < countYearB)
            continue;
          if (countMonthA < countMonthB)
            continue;
          if (countA <= countB)
            continue;
          filesArray[i] = valB;
          filesArray[i + 1] = valA;
        } catch (NumberFormatException nfe) {
          Log.log( ERROR1+nfe.getMessage() , NPSLOGGER );
        }
      }
      len--;
    }
    len = filesArray.length - 1;
    while (len > 0) {
      for (i = 0; i < len; i++) {
        valA = filesArray[i];
        valB = filesArray[i + 1];
        try {
          countYearA = Integer.parseInt(valA.substring(0, 4));
          countYearB = Integer.parseInt(valB.substring(0, 4));
          countMonthA = Integer.parseInt(valA.substring(4, 6));
          countMonthB = Integer.parseInt(valB.substring(4, 6));
          countDayA = Integer.parseInt(valA.substring(6, 8));
          countDayB = Integer.parseInt(valB.substring(6, 8));
          countA = Integer.parseInt(valA.substring(8, 10));
          countB = Integer.parseInt(valB.substring(8, 10));
          if (countYearA < countYearB)
            continue;
          if (countMonthA < countMonthB)
            continue;
          if (countDayA < countDayB)
            continue;
          if (countA <= countB)
            continue;
          filesArray[i] = valB;
          filesArray[i + 1] = valA;
        } catch (NumberFormatException nfe) {
          Log.log( ERROR1+nfe.getMessage() , NPSLOGGER );
        }
      }
      len--;
    }
    len = filesArray.length;
    for (i = 0; i < len; i++)
      filesVector.add(i, filesArray[i]);
  }
[Advertisement] BuildMaster allows you to create a self-service release management platform that allows different teams to manage their applications. Explore how!