PDA

View Full Version : Sorting data.


fx.barrett
04-21-2009, 09:11 AM
I need a bit of help/advice on the following problem:

1) I have a FileSystemDataGrid that has 3 columns, each column having custom labelFunction and sortCompareFunction.
2) I want to sort the data in the columns ( since it's a FileSystemDataGrid, the stuff actually displays files on the users' system ) but I want to somehow keep the folders always on top, no matter the direction of the sort ( like in Total Commander ).

Could anyone point me in an efficient direction? I'm curious if there's an easy way to do this without having to store the data in a huge array, then assign it as a data provider and stuff like that... I'm looking for a more efficient solution.

PS: I know this is not a 100% Flex related problem but I think that my chances of getting an answer for the problem are a bit higher here than in the AIR forum... anyway, this is more of a "theoretical thingie".

Thanks.

wvxvw
04-21-2009, 11:11 AM
Why won't you make it part of the condition in the sorting function?
I.e. when you need to sort the files there'll be function called like this:
function someSort(a:File, b:File):int
{
if ((a.isDirectory() && b.isDirectory()) || (!a.isDirectory() && !b.isDirectory()))
{
// do the sorting by alphabet, date etc.
}
if (!b.isDirectory() && a.isDirectory()) return 1;
if (!a.isDirectory() && b.isDirectory() return -1;
}
Not sure the arguments will be files, but, they will have to provide info such as isDirectory() or something...

fx.barrett
04-21-2009, 12:15 PM
Thanks for the reply. I first tried playing around with something like you posted but it didn't work... then I tried N other ways and still with no result. It's totally frustrating...

I'll give your code a shot too... hopefully, it will work...

fx.barrett
04-21-2009, 12:26 PM
Nah, I can't make it work... Too tired and frustrated because I've been wasting my time with this crap since this morning... Here's how one of the compare functions looks like:

private function compareNames(item1:File, item2:File):int
{
var newItem1:Object = {type: item1.isDirectory, name: item1.name};
var newItem2:Object = {type: item2.isDirectory, name: item2.name};

var collection:ArrayCollection = new ArrayCollection();
collection.addItem(newItem1);
collection.addItem(newItem2);

var collectionSort:Sort = new Sort();
collectionSort.fields = [new SortField("type", false), new SortField("name", true)];
collection.sort = collectionSort;
collection.refresh();

if (collection.getItemIndex(newItem1) > collection.getItemIndex(newItem2)) return -1;
else if (collection.getItemIndex(newItem1) < collection.getItemIndex(newItem2)) return 1;
else return 0;
}

I have no bloody idea how to sort those items correctly...

EDIT: Another thing that I was trying is to somehow sort the data again after the compareFunction finishes but this time only after type ( so I can push directories to the top ) but again, no luck...

One more idea that came to my mind is to somehow determine the direction of the sort... if I know the direction then I could associate it with the SortField and hopefully solve the problem... or something... I'm really lost...

wvxvw
04-21-2009, 12:45 PM
Can you just sort the ArrayCollection.source and then refresh the collection? I find this Sort thing very uncomfortable and compilcating...

fx.barrett
04-21-2009, 12:59 PM
EDIT: :D Ok, sorting the source and the stuff I'm doing seems quite the same... What's the difference ( except that with the source you are working with an Array and with the Sort you are working with an ArrayCollection )?

I think I'm asking too much here: could you perhaps throw together an extremely fast example? Just place a FileSystemDataGrid into a new AIR application and see if you can sort the name column so the folders remain on the top no matter the sort direction ( ASC or DESC ).

fx.barrett
04-21-2009, 04:19 PM
Ok, nevermind. Problem solved with the help of your first post... Everything was cool just that my sorting was bad... had to check for DESC or ASC first to reverse the stuff...

wvxvw
04-21-2009, 04:42 PM
Cool :) I was just lazy to start yet another project to try it... glad it was solved before I even started :)

fx.barrett
04-22-2009, 07:17 AM
In case someone else runs into something liks this then the code would look something like this:

private function compareNames(item1:File, item2:File):int
{
if((item1.isDirectory && item2.isDirectory) ||
(!item1.isDirectory && !item2.isDirectory))
{
var itemName1:String = item1.name.toLowerCase();
var itemName2:String = item2.name.toLowerCase();

if(itemName1 == itemName2) return 0;
else if(itemName1 > itemName2) return 1;
else return -1;

}else if(item1.isDirectory && !item2.isDirectory)
{
if(dgcolName.sortDescending)
return 1;
else
return -1;
}
else if(!item1.isDirectory && item2.isDirectory)
{
if(dgcolName.sortDescending)
return -1;
else
return 1;
}
else
return 0;
}

"dgcolName" is the ID of the column you are trying to sort.