Ruby 버전에 이어서 Groovy 버전도 만들어보았다. 거의 생김새가 비슷한 언어여서, 몇가지만 수정하면 바로 동작한다.

def FOLDER_LIST = [
                    "/path/to1",
                    "/path/to2",
                    "/path/to3",
                    "/path/to4",
                    "/path/to5",
                  ]

def startDate = new Date().format('yyyy/MM/dd HH:mm:ss')

def size_folder_list = []
FOLDER_LIST.each { folder_name ->
    def folder_size = 0
    new File(folder_name).eachFileRecurse { path ->
        folder_size += path.size()
    }
    size_folder_list << "${folder_size}|${folder_name}"
}

size_folder_list = size_folder_list.sort { a, b ->
    (a_size, a_file) = a.split(/\|/)
    (b_size, b_file) = b.split(/\|/)
    // b_size as int <=> a_size as int
    b_size.toInteger() <=> a_size.toInteger()
}

size_folder_list.each { folder ->
    (f_size, f_path) = folder.split(/\|/)
    println("${f_size},${f_path}")
}


def endDate = new Date().format('yyyy/MM/dd HH:mm:ss')
println "\n\n\n"
println "================================================================================"
println "Start Time : $startDate"
println "End   Time : $endDate"
println "================================================================================"

Python 에서 Dictionary라는 자료구조가 제공되는데, 이것은 Key 와 Value 로 요소가 이루어진다. 이번에 간단한 프로그램을 만들다가 Value 를 기준으로 Dictionary를 정렬할 필요가 생겼는데, 그 방법을 찾아보니 아래와 같았다. 이는 Python 2.4 이상에서 지원이 된다고 한다.

[코드]
#!/usr/bin/env python
# filename : dict_sort.py
from operator import itemgetter
 
dict = {}
dict['a'] = 2
dict['b'] = 1
dict['c'] = 5
 
print(sorted(dict.iteritems(), key=itemgetter(1), reverse=True))

[결과]
# python dict_sort.py
[('c', 5), ('a', 2), ('b', 1)]

+ Recent posts