github.com/searKing/golang/go@v1.2.117/os/sort.go (about)

     1  // Copyright 2021 The searKing Author. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package os
     6  
     7  import "os"
     8  
     9  // FileModeTimeSlice sorts filenames by mode time in increase order
    10  type FileModeTimeSlice []string
    11  
    12  func (s FileModeTimeSlice) Len() int {
    13  	return len(s)
    14  }
    15  func (s FileModeTimeSlice) Swap(i, j int) {
    16  	s[i], s[j] = s[j], s[i]
    17  }
    18  
    19  func (s FileModeTimeSlice) Less(i, j int) bool {
    20  	fi, err := os.Stat(s[i])
    21  	if err != nil {
    22  		return false
    23  	}
    24  	fj, err := os.Stat(s[j])
    25  	if err != nil {
    26  		return false
    27  	}
    28  	return fi.ModTime().Before(fj.ModTime())
    29  }
    30  
    31  // FileModeTimeDescSlice sorts filenames by mode time in decrease order
    32  type FileModeTimeDescSlice []string
    33  
    34  func (s FileModeTimeDescSlice) Len() int {
    35  	return len(s)
    36  }
    37  func (s FileModeTimeDescSlice) Swap(i, j int) {
    38  	s[i], s[j] = s[j], s[i]
    39  }
    40  
    41  func (s FileModeTimeDescSlice) Less(i, j int) bool {
    42  	fi, err := os.Stat(s[i])
    43  	if err != nil {
    44  		return false
    45  	}
    46  	fj, err := os.Stat(s[j])
    47  	if err != nil {
    48  		return false
    49  	}
    50  	return fi.ModTime().After(fj.ModTime())
    51  }