Page 1 of 1

sort in v10.7.0

Posted: Wed Jun 29, 2016 9:26 am
by csfreebird
1 description

I want to find oldest files when free space is not enough and delete them until free space is enough. Thus I find all files under one folder tree recursively and sort them by modified time.

2 code

2.1 find all files and save them into one list called file-list

(define (add-file-list f)
(push f file-list -1)
)

(define (find-all-files clean-folder)
(setq file-list '())
(recursive-access-dir clean-folder add-file-list)
file-list
)

;; @arg dir-path must be ended with /
(define (recursive-access-dir dir-path file-op)
(dolist (nde (directory dir-path {^[^.]}))
(if (directory? (append dir-path nde))
(recursive-access-dir (append dir-path nde "/") file-op)
(file-op (append dir-path nde)))))


2.2 sort file-list using compare function

(define (compare-time file1 file2)
(letn (t1 (int ((file-info file1) 6))
t2 (int ((file-info file2) 6))
)
(<= t1 t2)
)
)

(sort files compare-time)
3 result

The following code print all files' modified time

(setq files (find-all-files clean-folder))
(dolist (f files)
(println "files: " f ", time: " ((file-info f) 6))
The second file usa.sh is newer than jstc.sh, but it's in fron of jstc.sh. I check my code a few times, guess this is a bug of sort function.

files: /home/dean/sshclient/nj/delotto.sh, time: 1459176252
files: /home/dean/sshclient/nj/usa.sh, time: 1459176253
files: /home/dean/sshclient/nj/jstc.sh, time: 1459176252
files: /home/dean/sshclient/nj/test.sh, time: 1459176243
files: /home/dean/sshclient/nj/tunnel_gitlab_nanjing.sh, time: 1459176252
files: /home/dean/sshclient/nj/mongodb.sh~, time: 1459176252
files: /home/dean/sshclient/nj/root@/train.html~, time: 1461292073
files: /home/dean/sshclient/nj/root@/theme-readtheorg-local.setup, time: 1461292073
files: /home/dean/sshclient/nj/root@/package.sh, time: 1461292073
files: /home/dean/sshclient/nj/root@/imgs/in/rec1.png, time: 1461292073
files: /home/dean/sshclient/nj/root@/imgs/in/rec1.dot~, time: 1461292073
files: /home/dean/sshclient/nj/root@/imgs/in/path1.dot, time: 1461292073
files: /home/dean/sshclient/nj/root@/imgs/in/path1.dot~, time: 1461292073
files: /home/dean/sshclient/nj/root@/imgs/in/path1.png, time: 1461292073
files: /home/dean/sshclient/nj/root@/imgs/in/rec1.dot, time: 1461292073
files: /home/dean/sshclient/nj/root@/imgs/newbie/colla/u1.dot, time: 1461292073
files: /home/dean/sshclient/nj/root@/imgs/newbie/colla/u2.png, time: 1461292073
files: /home/dean/sshclient/nj/root@/imgs/newbie/colla/u1.png, time: 1461292073
files: /home/dean/sshclient/nj/root@/imgs/newbie/colla/u2.dot, time: 1461292073
files: /home/dean/sshclient/nj/root@/js/bootstrap.min.js, time: 1461292073
files: /home/dean/sshclient/nj/root@/js/jquery.min.js, time: 1461292073
files: /home/dean/sshclient/nj/root@/package.sh~, time: 1461292073
files: /home/dean/sshclient/nj/root@/train.html, time: 1461292073
files: /home/dean/sshclient/nj/root@/graph-schema/in_graph.dot, time: 1461292073
files: /home/dean/sshclient/nj/root@/graph-schema/in_graph.dot~, time: 1461292073
files: /home/dean/sshclient/nj/root@/graph-schema/ddl-to-dot.lsp~, time: 1461292073
files: /home/dean/sshclient/nj/root@/graph-schema/ddl.sql, time: 1461292073
files: /home/dean/sshclient/nj/root@/graph-schema/convert.lsp, time: 1461292073
files: /home/dean/sshclient/nj/root@/graph-schema/graph.dot~, time: 1461292073
files: /home/dean/sshclient/nj/root@/graph-schema/graph.png, time: 1461292073
files: /home/dean/sshclient/nj/root@/graph-schema/in_graph.png, time: 1461292073
files: /home/dean/sshclient/nj/root@/graph-schema/convert.lsp~, time: 1461292073
files: /home/dean/sshclient/nj/root@/styles/lib/js/jquery.stickytableheaders.min.js, time: 1461292073
files: /home/dean/sshclient/nj/root@/styles/lib/js/stickytableheaders-license.txt, time: 1461292073
Created: 2016-06-29 Wed 17:23

Re: [bug]sort in v10.7.0

Posted: Thu Jun 30, 2016 6:46 am
by ralph.ronnquist
The expression

Code: Select all

(sort files compare-time)
is kind of dangling; at the time of execution files has no value, and the result of the sorting is not captured anyhow.

Perhaps you meant to have that expression as part of the result creation step 3:

Code: Select all

(setq files (sort (find-all-files clean-folder) compare-time))

Re: [bug]sort in v10.7.0

Posted: Thu Jun 30, 2016 7:38 am
by csfreebird
Thanks. It's my bad. I use wrong variable files, but should be file-list.
There is no bug in sort function. My bug.