Wednesday, July 13, 2016

[Resolved] : Sorting according to File Size into Actionscript

[Resolved] : Sorting according to File Size into Actionscript

By default your File size comes into KB,MB,GB etc. format If you want to sort the data according to file size then you need to follow the below steps.

Visionfortech,Pratik Soni Blogs,Latest Technological Blog,Tech Blog


1) Find the Data Provider in which data Comes or we can say in which is data are there.
   
               Find the Component in which data is getting load and find the flow that at where data comes      or we can say the Data Provider of that Component.

2) Get the Property or we can say attribute into which our File size are getting stored so that            Fields have data into Human Readable Format Which is KB,MB,GB,TB,etc.etc....

                When you are going to Dubug that .swf File(Swift File) at that time you need to create               watch  expression on that Object and after passing from that line see what are the values comes           up into that Object and from that you will get the Field name or we can say Property or Attribute         in which File size is Coming.

3) Then Write a Function Which Converts the Human Readable Format (KB,MB,GB) data            into Bytes.

        Public function convertToBytes(filesize:String):Number
{
var filesize_number:Number = Number(filesize.substr(0,-2));

switch (filesize.substr(-2).toUpperCase())
{
case "KB":
return filesize_number*1024;
case "MB":
return filesize_number*Math.pow(1024,2);
case "GB":
return filesize_number*Math.pow(1024,3);
                   case "TB":
return filesize_number*Math.pow(1024,4);
default:
return Number(filesize);
}
}
4) After Conversion of File Size into Bytes we need to check that Data Provider is of Which type     Array,ArrayCollection,XMLListCollection etc...So that We can Sort accordingly Because in       Array You can Swap the Object Easily but into XMLListCollection data comes into XML           Format So You can't do Easy Swap into that.

    If it is Array or ArrayCollection

   Ascending :

   Public function sortBySize(event:DataGridEvent):void
   {
      var file_grid:Array= new Array(FileManagerModelLocator.getInstance().files.length);
     for (var i:int = 0;i < FileManagerModelLocator.getInstance().files.length ; i++)
     {
var temp:Object = new Object();
temp = FileManagerModelLocator.getInstance().files[i];
var k:int = 0;
for (var j:int = 0;j < FileManagerModelLocator.getInstance().files.length ; j++)
{
if(convertToBytes(FileManagerModelLocator.getInstance().files[i].totalFileSizeinMB) > convertToBytes(FileManagerModelLocator.getInstance().files[j].totalFileSizeinMB))
{
temp = convertToBytes(FileManagerModelLocator.getInstance().files[i].totalFileSizeinMB);
convertToBytes(FileManagerModelLocator.getInstance().files[i].totalFileSizeinMB) = convertToBytes(FileManagerModelLocator.getInstance().files[j].totalFileSizeinMB);
convertToBytes(FileManagerModelLocator.getInstance().files[j].totalFileSizeinMB) = temp;
}
}

file_grid[k] = (FileManagerModelLocator.getInstance().files[i]);
 }
    trace(FileManagerModelLocator.getInstance().files);
    }

    Descending :

  Public function sortBySize(event:DataGridEvent):void
  {
    var file_grid:Array= new Array(FileManagerModelLocator.getInstance().files.length);
   for (var i:int = 0;i < FileManagerModelLocator.getInstance().files.length ; i++)
   {
var temp:Object = new Object();
temp = FileManagerModelLocator.getInstance().files[i];
var k:int = 0;
for (var j:int = 0;j < FileManagerModelLocator.getInstance().files.length ; j++)
{
if(convertToBytes(FileManagerModelLocator.getInstance().files[i].totalFileSizeinMB) <                         convertToBytes(FileManagerModelLocator.getInstance().files[j].totalFileSizeinMB))
{
temp = convertToBytes(FileManagerModelLocator.getInstance().files[i].totalFileSizeinMB);
convertToBytes(FileManagerModelLocator.getInstance().files[i].totalFileSizeinMB) =                         convertToBytes(FileManagerModelLocator.getInstance().files[j].totalFileSizeinMB);
convertToBytes(FileManagerModelLocator.getInstance().files[j].totalFileSizeinMB) = temp;
}
}

file_grid[k] = (FileManagerModelLocator.getInstance().files[i]);
  }
    trace(FileManagerModelLocator.getInstance().files);
   }

    If it is XMLListCollection
 
    Ascending :

    Public function sortBySize(event:DataGridEvent):void
    {
        var file_grid:Array= new Array(FileManagerModelLocator.getInstance().files.length);
       for (var i:int = 0;i < FileManagerModelLocator.getInstance().files.length ; i++)
       {
var temp:Object = new Object();
temp = FileManagerModelLocator.getInstance().files[i];
var k:int = 0;
for (var j:int = 0;j < FileManagerModelLocator.getInstance().files.length ; j++)
{
if(convertToBytes(FileManagerModelLocator.getInstance().files[i].totalFileSizeinMB) >                         convertToBytes(FileManagerModelLocator.getInstance().files[j].totalFileSizeinMB))
{
k++;
}
}

             file_grid[k] = (FileManagerModelLocator.getInstance().files[i]);
         }
        trace(FileManagerModelLocator.getInstance().files);
     }

    Descending :

     Public function sortBySize(event:DataGridEvent):void
     {
        var file_grid:Array= new Array(FileManagerModelLocator.getInstance().files.length);
       for (var i:int = 0;i < FileManagerModelLocator.getInstance().files.length ; i++)
       {
var temp:Object = new Object();
temp = FileManagerModelLocator.getInstance().files[i];
var k:int = 0;

           for (var j:int = 0;j < FileManagerModelLocator.getInstance().files.length ; j++)
{
if(convertToBytes(FileManagerModelLocator.getInstance().files[i].totalFileSizeinMB) <                         convertToBytes(FileManagerModelLocator.getInstance().files[j].totalFileSizeinMB))
{
k++;
}
}

file_grid[k] = (FileManagerModelLocator.getInstance().files[i]);
    }
         trace(FileManagerModelLocator.getInstance().files);
        }

Now the Files or we can say Data Provider has sorted data.

Feel free to comment below your experience with above approach and If you still find any problem  with above steps Let me know I would love to help you to resolve your  problem.

 If you want to take your Technological Knowledge to the Next Level and For More Technological information Stay tuned to  visionfortech.blogspot.com


No comments:

Post a Comment

Protected by Copyscape