1. can i make this better?
- Posted by "Greg Haberek" <g.haberek at comcast.net> Feb 07, 2004
- 511 views
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? ~Greg
2. Re: can i make this better?
- Posted by "Derek Parnell" <ddparnell at bigpond.com> Feb 07, 2004
- 506 views
----- Original Message ----- From: "Greg Haberek" <g.haberek at comcast.net> To: <EUforum at topica.com> Subject: can i make this better? > > > 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? > > ~Greg function compare_dir(object x1, object x2) integer ret integer d1,d2 d1 = (find('d', x1[D_ATTRIBUTES]) != 0) d2 = (find('d', x2[D_ATTRIBUTES]) != 0) if d1 = d2 then ret = compare(lower(x1[D_NAME]), lower(x2[D_NAME])) else ret = d2 - d1 end if return ret end function > > > TOPICA - Start your own email discussion group. FREE! > >
3. Re: can i make this better?
- Posted by "Greg Haberek" <g.haberek at comcast.net> Feb 07, 2004
- 500 views
> function compare_dir(object x1, object x2) > integer ret > integer d1,d2 > > d1 = (find('d', x1[D_ATTRIBUTES]) != 0) > d2 = (find('d', x2[D_ATTRIBUTES]) != 0) > if d1 = d2 then > ret = compare(lower(x1[D_NAME]), lower(x2[D_NAME])) > else > ret = d2 - d1 > end if > > return ret > end function WOW! Thanks! ~Greg
4. Re: can i make this better?
- Posted by Pete Lomax <petelomax at blueyonder.co.uk> Feb 07, 2004
- 497 views
On Sat, 7 Feb 2004 14:26:39 -0500, Greg Haberek <g.haberek at comcast.net> 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: > I have not tested this at all, but off the top of my head something like this should work: function compare_dir(object x1, object x2) integer x1d x1d=find('d', x1[D_ATTRIBUTES])!=0 if x1d=find('d', x2[D_ATTRIBUTES])!=0 then return compare(lower(x1[D_NAME]), lower(x2[D_NAME])) end if if x1d then return -1 else return 1 end if end function Regards, Pete
5. Re: can i make this better?
- Posted by "Juergen Luethje" <j.lue at gmx.de> Feb 07, 2004
- 508 views
Greg 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 I believe I would code it like this (not tested), this way we only have to deal with 3 cases: function compare_dir (object x1, object x2) integer type1, type2 type1 = find('d', x1[D_ATTRIBUTES]) -- 0 => file, != 0 => dir type2 = find('d', x2[D_ATTRIBUTES]) if type1 = type2 then return compare(lower(x1[D_NAME]), lower(x2[D_NAME])) elsif type1 != 0 then return -1 else return 1 end if 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? > > ~Greg Regards, Juergen
6. Re: can i make this better?
- Posted by "Kat" <gertie at visionsix.com> Feb 07, 2004
- 504 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