Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Simple way to sort a list?

Status
Not open for further replies.

LilTCLer

Programmer
Nov 27, 2007
37
US
Hi,

I've read in the contents of a directory into a list as follows:

set tempdirContents [ lsort [ glob -nocomplain -types { d } -- * ] ]

What I want to do is sort the list based on the last modify time (so the last modify time is the last entry in the list).

Any ideas on a quick way to do this?

Thanks
 
Hi

Not especially elegant, but I would do it like this :
Code:
[b]proc[/b] comparator {a b} {
  [b]global[/b] filetime

  [b]if[/b] {$filetime($a) < $filetime($b)} {
    [b]return[/b] -1
  } [b]elseif[/b] {$filetime($a) > $filetime($b)} {
    [b]return[/b] 1
  } [b]else[/b] {
    [b]return[/b] 0
  }
}

[b]set[/b] filelist [ [b]glob[/b] -nocomplain -types { d } -- * ]

[b]foreach[/b] file $filelist {
  [b]set[/b] filetime($file) [[b]file[/b] mtime $file]
}

[b]set[/b] tempdirContents [ [b]lsort[/b] -command comparator $filelist ]

Feherke.
feherke.ga
 
Thank you, Ill give that a shot after I figure out what its exactly doing :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top