-1

I need you help to fin a solution to order an array by creation date time. I try this but this function seems to order only by date and not by time together.

private function triImgDate(folder:Array):void {
        var acDirectory:ArrayCollection = new ArrayCollection();

        for each (var tmp:File in folder) {
            if (!tmp.isDirectory) {
                if (tmp.extension != null && ArrayFunction.inArray(tabExt, tmp.extension.toLowerCase()))
                {

                    listeImages.addItem(tmp);

                }
            }
            else {
                triImgDate(tmp.getDirectoryListing());
            }
        }

        var dataSortField:SortField = new SortField();
        dataSortField.name = "creationDate";
        dataSortField.numeric = false;
        var numericDataSort:Sort = new Sort();
        numericDataSort.fields = [dataSortField];
        listeImages.sort = numericDataSort;
        listeImages.refresh();



    }

So if you can help me.

Best regards

// solution

private function triImgDate(folder:Array):void {


        var acDirectory:ArrayCollection = new ArrayCollection();
        var arTempListeImages:ArrayCollection = new ArrayCollection();
        var arTempListeCompar:ArrayCollection = new ArrayCollection();


        var objImg:Object;
        for each (var tmp:File in folder) {
            if (!tmp.isDirectory) {
                if (tmp.extension != null && ArrayFunction.inArray(tabExt, tmp.extension.toLowerCase())) {

                    objImg= new Object();
                    //listeImages.addItem(tmp);
                    arTempListeImages.addItem(tmp);
                    objImg.creationDate=tmp.creationDate;
                    objImg.url=tmp.url;
                    objImg.pos=arTempListeCompar.length;
                    arTempListeCompar.addItem(objImg)


                }
            } else {
                triImgDate(tmp.getDirectoryListing());
            }
        }

        var dataSortField:SortField = new SortField();
        dataSortField.name = "creationDate";
        dataSortField.compareFunction = function (a:Object, b:Object) : int {
            var na:Number = a.creationDate.getTime();
            var nb:Number = b.creationDate.getTime();

            if (na < nb)
                return -1;

            if (na > nb)
                return 1;

            return 0;
        };

        var numericDataSort:Sort = new Sort();
        numericDataSort.fields = [dataSortField];

        arTempListeCompar.sort= numericDataSort;
        arTempListeCompar.refresh();
        var cpt:int=0;
        for each (var objTem:Object in arTempListeCompar) {

            listeImages.addItem(arTempListeImages[arTempListeCompar[cpt].pos]);
            cpt++;
        }

        arTempListeImages=[];
        arTempListeCompar=[];
        acDirectory=[];

    }
Flex60460
  • 933
  • 2
  • 18
  • 49

1 Answers1

1

What you're trying to do here, is comparing twe dates by their string representation, which is obviously not a good way to do it.

you should rather use compareFunction property of the SortField object. Something like that (not tested):

private function triImgDate(folder:Array):void {
  var acDirectory:ArrayCollection = new ArrayCollection();
  for each (var tmp:File in folder) {
    if (!tmp.isDirectory) {
      if (tmp.extension != null && ArrayFunction.inArray(tabExt, tmp.extension.toLowerCase())) {
        listeImages.addItem(tmp);
      }
    } else {
      triImgDate(tmp.getDirectoryListing());
    }
  }

  var dataSortField:SortField = new SortField();
  dataSortField.name = "creationDate";
  dataSortField.sortFunction = function(date1:Date, date2:Date):int {
    const t1:uint = date1.time;
    const t2:uint = date2.time;
    if (t1 < t2) {
      return -1;
    }

    if (t1 == t2) {
      return 0;
    }

    return 1;
  }

  var numericDataSort:Sort = new Sort();
  numericDataSort.fields = [dataSortField];
  listeImages.sort = numericDataSort;
  listeImages.refresh();
}
leetwinski
  • 17,408
  • 2
  • 18
  • 42