如何按名称和日期对LinkedList <fileinfo>进行排序?

时间:2017-11-28 11:08:36

标签: java

我无法弄清楚如何使用fileinfo对链接列表进行排序,下面的代码显示了构建内容的过程&#34;,

 private void ReloadScanFiles() {
        if (gsfCurRow != null) {
            ArrayList<FileInfo> newTreeFileInfoList = new ArrayList<FileInfo>();
            ArrayList<FileInfo> addTreeFileInfoList = new ArrayList<FileInfo>();
            ArrayList<String> delTreeFileNameList = new ArrayList<String>();
            /////// linked list
            LinkedList<FileInfo> newTreeFileInfoLinkedList = new LinkedList<FileInfo>();
            LinkedList<FileInfo> addTreeFileInfoLinkedList = new LinkedList<FileInfo>();
            LinkedList<String> delTreeFileNameLinkedList = new LinkedList<String>();



            File folder = new File(gsfCurRow.DOC_SOURCE_PATH);

            FileFilter fileFilter = new FileFilter() {
                public boolean accept(File file) {
                    return !file.isDirectory() && !file.isHidden();
                }
            };
/*          
            FilenameFilter fileFilter = new FilenameFilter() {
                public boolean accept(File dir, String name) {
                    return name.toLowerCase().endsWith(".pdf") ||
                           name.toLowerCase().endsWith(".jpeg") ||
                           name.toLowerCase().endsWith(".jpg") ||
                           name.toLowerCase().endsWith(".tiff") ||
                           name.toLowerCase().endsWith(".txt");                    
                }
            };
*/
            File[] listOfFiles = folder.listFiles(fileFilter);          

            for (int i = 0; i < listOfFiles.length; i++){
                FileInfo fileInfo = new FileInfo();

                fileInfo.Name = listOfFiles[i].getName();
                //fileInfo.Length = (long) listOfFiles[i].length();
                fileInfo.LastModified = (long) listOfFiles[i].lastModified(); //Long.parseLong("473", 10);

                //newTreeFileInfoLinkedList.add(fileInfo);
                newTreeFileInfoLinkedList.add(fileInfo);
            }


            //add new items
            for (FileInfo fileInfo: newTreeFileInfoLinkedList){ 
            //for (FileInfo fileInfo: newTreeFileInfoLinkedList){       

                boolean existsItem = false;

                if (actTreeFileInfoLinkdList != null){
                    for (FileInfo fileInfo1: actTreeFileInfoLinkdList){
                        if (fileInfo.Name.equals(fileInfo1.Name)) {

                            existsItem = true;
                            break;
                        }
                    }
                }


                if (!existsItem) {
                    //addTreeFileInfoLinkedList.add(fileInfo);
                    addTreeFileInfoLinkedList.add(fileInfo);
                }

            }



            //delete non existing items

            if (actTreeFileInfoLinkdList != null){
                for (FileInfo fileInfo: actTreeFileInfoLinkdList){
                    boolean existsItem = false;

                    for (FileInfo fileInfo1: newTreeFileInfoLinkedList){
                        if (fileInfo.Name.equals(fileInfo1.Name)) {
                            existsItem = true;
                            break;
                        }
                    }


                    if (!existsItem) {
                        //delTreeFileNameLinkedList.add(fileInfo);
                        delTreeFileNameLinkedList.add(fileInfo.Name);
                    }
                }                   
            }

            actTreeFileInfoLinkdList = newTreeFileInfoLinkedList;



            //add items to tree
            for (FileInfo fileInfo: addTreeFileInfoLinkedList){
                TreeItem item = new TreeItem(tv, SWT.NONE); 
                //item.setText(new String[] {fileInfo.Name, Long.toString(fileInfo.Length), df.format(fileInfo.LastModified)});
                item.setText(new String[] {fileInfo.Name, null, df.format(fileInfo.LastModified)});             
                fileCount ++;                       
            }


            //delete item from tree
            //update locked by
            TreeItem [] items = tv.getItems();   
            for (TreeItem item: items){
                //delete
                if (delTreeFileNameLinkedList.contains(item.getText())) {
                    if (item.getChecked()) {
                        checkedFileCount --;                    
                    }
                    fileCount --;
                    item.dispose();
                }
                //update
                if (!item.isDisposed()){
                    Integer lock_idusr = null;
                    try {
                        lock_idusr = db.FileLockedBy(gsfCurRow.DOC_SOURCE_ID, item.getText());
                    } catch (SQLException e) {
                        e.printStackTrace();
                    }


                    if ((lock_idusr != null) && (!lock_idusr.equals(idusr))){
                        try {
                            item.setText(3, db.GetOpid(lock_idusr));
                        } catch (SQLException e) {
                            e.printStackTrace();
                        }
                        Color color = new Color(display,180, 0, 0);
                        item.setForeground(color);
                        item.setChecked(false);
                    } 
                    else{
                        item.setText(3, "");
                        Color color = new Color(display,0, 0, 0);
                        item.setForeground(color);                      
                    }
                }
            }

            //sort linked list files
            //actTreeFileInfoLinkdList




            BtnDocAssignEnabled();
            GenerateTreeStatusText();


        }
    }

您可以在下面的图片中看到输出: enter image description here

我需要根据他们的姓名或日期对这些项目进行排序,我认为将听众放在两个列上都是meno - 意思是名称,数据 - 表示日期。我如何实现这一目标?我怎样才能对LinkedList进行排序?

1 个答案:

答案 0 :(得分:0)

名称:

Collections.sort(newTreeFileInfoLinkedList, (f1, f2) -> {
    return f1.Name.compareTo(f2.Name);
});

要比较日期,只需将.Name替换为.LastModified

即可