Re: can i make this better?

new topic     » goto parent     » topic index » view thread      » older message » newer message

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

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu