簡體   English   中英

數組列表排序困難

[英]Trouble Sorting an array list

有沒有更好的方法可以對此進行排序,以獲得正確的訂單? 謝謝

import java.io.*;
import java.util.*;


public class JpgDirToHtm
{
    public static void main(String[] args) throws FileNotFoundException
    {




        Scanner kb = new Scanner(System.in);  
        System.out.print("Enter the path of the folder whose contents you wish to insert into an html file: ");
        String path = kb.nextLine(); 



        File folder = new File(path);
        File[] listOfFiles = folder.listFiles();
        ArrayList<String> htmlTextList = new ArrayList<String>();

        for (int i = 0; i < listOfFiles.length; i++)
        {
            if (listOfFiles[i].isFile())
            {
                htmlTextList.add(listOfFiles[i].getName() );
            }


        }

        Collections.sort(htmlTextList);

        System.out.println(htmlTextList);
   }
}

這就是它打印的內容[1.jpeg,10.jpg,11.jpeg,12.jpeg,13.jpeg,14.jpeg,16.jpg,17.jpg,18.jpg,19.jpg,2.jpeg, 20.jpg,21.jpg,22.jpg,23.jpg,24.jpg,25.jpg,3.jpg,5.jpg,7.jpeg,9.jpg]

我需要2.jpeg才能加入1.jpeg等。

抱歉,可能有一個簡單的修復程序,但我在Google上找不到任何東西。 我是編程新手。

其他一切都很好。 整個程序可以拍攝數千張照片,並以正確的大小自動將它們放置在html網頁中,並且每頁可以設置給定數量的照片。 如果有人對剩下的代碼感興趣,我將其發布。

編寫自己的比較器:

    Collections.sort(list, new Comparator<String>() {

        @Override
        public int compare(String o1, String o2) {
            String filename1 =o1.substring(0,o1.indexOf("."));
            String filename2 =o2.substring(0,o2.indexOf("."));
            return Integer.valueOf(filename1).compareTo(Integer.valueOf(filename2));
        }
    });

這會將文件名轉換為整數並進行比較。 但是請注意,僅當文件名是數字時才有效!

您將必須編寫自己的比較器,並注意以數字或字符串開頭的flie場景:

public class JpgDirToHtm
{

    public static void main(String[] args) throws FileNotFoundException
    {
        Scanner kb = new Scanner(System.in);  
        System.out.print("Enter the path of the folder whose contents you wish to insert into an html file: ");
        String path = kb.nextLine(); 

        File folder = new File(path);
        File[] listOfFiles = folder.listFiles();
        ArrayList<String> htmlTextList = new ArrayList<String>();

        for (int i = 0; i < listOfFiles.length; i++)
        {
            if (listOfFiles[i].isFile())
            {
                htmlTextList.add(listOfFiles[i].getName() );
            }


        }

        Collections.sort(htmlTextList, new Sortbyname());

        System.out.println(htmlTextList);
    }
}

class Sortbyname implements Comparator<String> 
{ 
    // Used for sorting in ascending order of 
    // roll name 
    public int compare(String a, String b) 
    { 
        String tempA = a.split("\\.")[0];
        String tempB = b.split("\\.")[0];

        try
        {
            return Integer.parseInt(tempA)-Integer.parseInt(tempB);
        }
        catch (Exception e)
        {
            return a.compareTo(b); 
        }
    } 
} 

該代碼捕獲格式化數字的任何異常,只是回退到String compare。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM