Nicke
Nicke

Reputation: 365

FileSystemList sorting by date

I've got this FileSystemList that, obviously, list files from a specified directory. All works great except that I want to sort the listing based on created date for the files.

Is this possible for a FileSystemList?

Upvotes: 0

Views: 125

Answers (1)

splash
splash

Reputation: 13327

Out of the box you can only sort by file name via the nameCompareFunction property. Unfortunately the API of the FileSystem* controls is not well designed for extensibility or customization. So the things become a "little bit" more complicated and you have to derive from FileSystemList:

com.stackoverflow.MyFileSystemList

package com.stackoverflow
{
    import mx.controls.FileSystemList;
    import mx.core.mx_internal;

    use namespace mx_internal;

    public class MyFileSystemList extends FileSystemList
    {
        public function MyFileSystemList()
        {
            super();
            // overwrite the FileSystemControlHelper instance                
            helper = new MyFileSystemControlHelper(this, false);
            iconFunction = helper.fileIconFunction;
            labelFunction = helper.fileLabelFunction;
            directory = COMPUTER;
        }

        public function set fileCompareFunction(value:Function): void
        {
            MyFileSystemControlHelper(helper).fileCompareFunction = value;
        }

        public function get fileCompareFunction():Function
        {
            return MyFileSystemControlHelper(helper).fileCompareFunction;
        }
    }
}

com.stackoverflow.MyFileSystemControlHelper

package com.stackoverflow
{
    import mx.controls.fileSystemClasses.FileSystemControlHelper;
    import mx.core.mx_internal;

    use namespace mx_internal;

    public class MyFileSystemControlHelper extends FileSystemControlHelper
    {
        public function MyFileSystemControlHelper(owner:Object, hierarchical:Boolean)
        {
            super(owner, hierarchical);                
            directoryEnumeration = new MyDirectoryEnumeration();
        }

        private var _customFileCompareFunction:Function;            
        private var customFileCompareFunctionChanged:Boolean = false;

        public function set fileCompareFunction(value:Function): void
        {
            _customFileCompareFunction = value;
            customFileCompareFunctionChanged = true;
            owner.invalidateProperties();
        }

        public function get fileCompareFunction():Function
        {
            return _customFileCompareFunction;
        }

        override public function commitProperties():void
        {
            super.commitProperties();                
            if (customFileCompareFunctionChanged)
            {
                MyDirectoryEnumeration(directoryEnumeration).customFileCompareFunction = fileCompareFunction;
                directoryEnumeration.refresh();
                owner.dataProvider = directoryEnumeration.collection;
                itemsChanged();
                customFileCompareFunctionChanged = false;
            }
        }
    }
}

com.stackoverflow.MyDirectoryEnumeration

package com.stackoverflow
{
    import flash.filesystem.File;

    import mx.utils.DirectoryEnumeration;
    import mx.utils.DirectoryEnumerationMode;

    public class MyDirectoryEnumeration extends DirectoryEnumeration
    {
        public function MyDirectoryEnumeration(source:Array = null)
        {
            super(source);
        }

        private var _fileCompareFunction:Function;

        public function get customFileCompareFunction(): Function
        {
            return _fileCompareFunction;
        }

        public function set customFileCompareFunction(value: Function):void
        {
            _fileCompareFunction = value;
        }

        override public function fileCompareFunction(file1:File, file2:File, fields:Array=null):int
        {
            if (_fileCompareFunction == null)
                return super.fileCompareFunction(file1, file2, fields);

            if (file1.isDirectory && !file2.isDirectory)
            {
                if (enumerationMode == DirectoryEnumerationMode.DIRECTORIES_FIRST)
                    return -1;
                if (enumerationMode == DirectoryEnumerationMode.FILES_FIRST)
                    return 1;
            }

            if (!file1.isDirectory && file2.isDirectory)
            {
                if (enumerationMode == DirectoryEnumerationMode.DIRECTORIES_FIRST)
                    return 1;
                if (enumerationMode == DirectoryEnumerationMode.FILES_FIRST)
                    return -1;
            }

            return _fileCompareFunction(file1, file2);
        }
    }
}

Afterwards you can use your MyFileSystemList component and provide your own compare function to sort the files based on creation date:

<?xml version="1.0" encoding="utf-8"?>
<s:WindowedApplication xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx" xmlns:stackoverflow="com.stackoverflow.*">

    <stackoverflow:MyFileSystemList fileCompareFunction="compareFiles"/>

<fx:Script><![CDATA[
    private function compareFiles(file1:File, file2:File):int
    {
        if (file1.creationDate.time > file2.creationDate.time)
            return 1;
        else
            return -1;
    }

]]></fx:Script>
</s:WindowedApplication>

Upvotes: 1

Related Questions