如何根据扩展对包含文件的arrayList进行排序

时间:2018-04-04 10:48:03

标签: java arrays sorting collections

File [] templatesList = templateFileList.toArray(new File[0] );
Arrays.sort(templatesList,ExtensionFileComparator.EXTENSION_INSENSITIVE_REVERSE);

templatesList包含

等文件
  • app.vm
  • app.1.ivm
  • app.2.ivm
  • mart.ivm
  • mart.vm

基于最后一次扩展的第一次反向排序, 然后根据basename排序, 然后排序数字。 排序后,预期输出

  • app.vm
  • mart.vm
  • app.1.ivm
  • app.2.ivm
  • mart.ivm

我的代码:

    File [] templatesList = templateFileList.toArray(new File[0] );
    List <File> tempList = Arrays.asList(templatesList);
    tempList.stream().sorted(ExtensionFileComparator.EXTENSION_INSENSITIVE_REVERSE.
            thenComparing(NameFileComparator.NAME_INSENSITIVE_COMPARATOR));     

        List <File> templateFileList1 = new ArrayList(Arrays.asList(templatesList));
        Collections.sort(templateFileList1, new Comparator <File>() {

            @Override
            public int compare(File f1, File f2) {
                return Integer.parseInt(FilenameUtils.getExtension(FilenameUtils.getBaseName(f1.toString())))-
                        Integer.parseInt(FilenameUtils.getExtension(FilenameUtils.getBaseName(f2.toString())));
            }
        });

2 个答案:

答案 0 :(得分:1)

您可以混合来自Apache commons if ((input->isKeyDown(sf::Keyboard::D)) && (!isSliding) && (hDir != 0)) //slide: if slide is pressed AND cooldown is finished. { cout << "\nSlide pressed!"; cout << "\nElapsed slide time: " + to_string(slideElapsedTime); if (playerStamina >= slideCost) { //isAttacking = true; isSliding = true; slideElapsedTime = 0; input->setKeyUp(sf::Keyboard::D); if (walk.getFlipped()) //if walk is flipped then flip the slide animation. { duck.setFlipped(true); } else duck.setFlipped(false); currentAnimation = &duck; //using duck animation for teleport animation. currentAnimation->animate(dt); currentAnimation->reset(); setTextureRect(currentAnimation->getCurrentFrame()); if (isSliding) { target = sf::Vector2f(getPosition().x + 700 * hDir, getPosition().y); //set target location (current location + offset) direction = target - getPosition(); //set direction to travel in (depends on current position) if (getPosition() != target) //if not at same location... { move(direction*dt*scale); //...then move to target location. } } playerStamina -= slideCost; //reduce the stamina damage = 2; //set damage to be dealt to enemy. //check collision and apply damage } } 的{​​{1}}和ExtensionFileComparator 使用Java 8:

NameFileComparator

或者在没有任何API的情况下使用更多的锅炉板代码来编写它。

答案 1 :(得分:0)

如果有人在寻找答案

File [] templatesList = templateFileList.toArray(new File[0] );
    Arrays.sort(templatesList, ExtensionFileComparator.EXTENSION_INSENSITIVE_REVERSE
            .thenComparing(NameFileComparator.NAME_INSENSITIVE_COMPARATOR).thenComparing(new Comparator <File>() {
                @Override
                public int compare(File f1, File f2) {
                    String file1 = FilenameUtils.getBaseName(f1.toString());
                    String file2 = FilenameUtils.getBaseName(f2.toString());

                    String ext1 = FilenameUtils.getExtension(file1);
                    String ext2 = FilenameUtils.getExtension(file2);

                    if ((StringUtils.isNumericSpace(ext1)) &&(StringUtils.isNumericSpace(ext2))) {
                        int t1= Integer.parseInt(ext1);
                        int t2= Integer.parseInt(ext2);
                        return t1-t2;
                    }
                    else {
                    return 0;
                    }
                }                   
            }));