Re: can i make this better?
- Posted by "Kat" <gertie at visionsix.com> Feb 07, 2004
- 503 views
On 7 Feb 2004, at 14:26, Greg Haberek wrote: > > > I want to sort the results from dir() so that all the directories are first, > then the files, in alphabetical order. I'm using custom_sort() and compare(), > so > far this is what I have: > > function compare_dir(object x1, object x2) > integer ret > > if find('d', x1[D_ATTRIBUTES]) then > if find('d', x2[D_ATTRIBUTES]) then > ret = compare(lower(x1[D_NAME]), lower(x2[D_NAME])) > else > ret = -1 > end if > else > if find('d', x2[D_ATTRIBUTES]) then > ret = 1 > else > ret = compare(lower(x1[D_NAME]), lower(x2[D_NAME])) > end if > end if > > return ret > end function > > function sort_dir(sequence x) > return custom_sort(routine_id("compare_dir"), x) > end function > > > Right now this works, but I feel all those if statements could be more > efficient, it just looks messy to me. Can anyone patch this up? I used sorttok() for that once upon a time, but i can't find the code right now. It allowed me to sort by size, name, type, date, etc. You can sort by type, and inside that by alphabet and in that by size. Or invert each nested sort if you like. Simple as sorted_dir = sorttok(dir_list,{3,-2},"\t\n\r") or such, i wish i could find the code i did way back when.... but see the readme.html in strtok for examples. Kat