github.com/wangyougui/gf/v2@v2.6.5/os/gfile/gfile_sort.go (about) 1 // Copyright GoFrame Author(https://goframe.org). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/wangyougui/gf. 6 7 package gfile 8 9 import ( 10 "strings" 11 12 "github.com/wangyougui/gf/v2/container/garray" 13 ) 14 15 // fileSortFunc is the comparison function for files. 16 // It sorts the array in order of: directory -> file. 17 // If `path1` and `path2` are the same type, it then sorts them as strings. 18 func fileSortFunc(path1, path2 string) int { 19 isDirPath1 := IsDir(path1) 20 isDirPath2 := IsDir(path2) 21 if isDirPath1 && !isDirPath2 { 22 return -1 23 } 24 if !isDirPath1 && isDirPath2 { 25 return 1 26 } 27 if n := strings.Compare(path1, path2); n != 0 { 28 return n 29 } else { 30 return -1 31 } 32 } 33 34 // SortFiles sorts the `files` in order of: directory -> file. 35 // Note that the item of `files` should be absolute path. 36 func SortFiles(files []string) []string { 37 array := garray.NewSortedStrArrayComparator(fileSortFunc) 38 array.Add(files...) 39 return array.Slice() 40 }