Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems sorting a 2-dimensional array

I want to get the filename of the latest file in a directory. latest is based on creation time.

Currently I stuck in sorting the 2-dimensional array. I do not know how I should sort it? I get the following error

ERROR: LoadError: MethodError: no method matching isless(::Array{Any,1}, ::Array{Any,1})

The 2-dimensional array looks like this:

Any[
    Any[1.47913e9,"foo.csv"],
    Any[1.47913e9,"bar.csv"],
    Any[1.47913e9,"foobar.csv"]
]

newestfile.jl

dfolder = "C:\\Users\\Foo\\Downloads"
cd( dfolder )
dfiles = readdir( "." )

files=[]

#println( dfiles )
for file in dfiles
    created = ctime( file )
    push!(files, [created, file]  )
end

println( files )

# sort the timestamp
sort!( files ) # This throws an error

# grab the newst file and display the filename

How do I display the newest file in the directory?

like image 240
jerik Avatar asked Feb 22 '26 00:02

jerik


1 Answers

Try:

julia> sort!( files, by = e -> e[1])

The last item is the newest one:

julia> files[end]
2-element Array{Any,1}:
1.48061e9 ".whatever"

The filename is:

julia> files[end][2]
".whatever"
like image 119
daycaster Avatar answered Feb 24 '26 15:02

daycaster



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!