2
* Licensed to the Apache Software Foundation (ASF) under one or more
3
* contributor license agreements. See the NOTICE file distributed with
4
* this work for additional information regarding copyright ownership.
5
* The ASF licenses this file to You under the Apache License, Version 2.0
6
* (the "License"); you may not use this file except in compliance with
7
* the License. You may obtain a copy of the License at
9
* http://www.apache.org/licenses/LICENSE-2.0
11
* Unless required by applicable law or agreed to in writing, software
12
* distributed under the License is distributed on an "AS IS" BASIS,
13
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
* See the License for the specific language governing permissions and
15
* limitations under the License.
17
package org.apache.commons.io.comparator;
20
import java.io.Serializable;
21
import java.util.Comparator;
24
* Compare the <b>last modified date/time</b> of two files for order
25
* (see {@link File#lastModified()}).
27
* This comparator can be used to sort lists or arrays of files
28
* by their last modified date/time.
30
* Example of sorting a list of files using the
31
* {@link #LASTMODIFIED_COMPARATOR} singleton instance:
33
* List<File> list = ...
34
* Collections.sort(list, LastModifiedFileComparator.LASTMODIFIED_COMPARATOR);
37
* Example of doing a <i>reverse</i> sort of an array of files using the
38
* {@link #LASTMODIFIED_REVERSE} singleton instance:
41
* Arrays.sort(array, LastModifiedFileComparator.LASTMODIFIED_REVERSE);
45
* @version $Revision: 609243 $ $Date: 2008-01-06 00:30:42 +0000 (Sun, 06 Jan 2008) $
46
* @since Commons IO 1.4
48
public class LastModifiedFileComparator implements Comparator, Serializable {
50
/** Last modified comparator instance */
51
public static final Comparator LASTMODIFIED_COMPARATOR = new LastModifiedFileComparator();
53
/** Reverse last modified comparator instance */
54
public static final Comparator LASTMODIFIED_REVERSE = new ReverseComparator(LASTMODIFIED_COMPARATOR);
57
* Compare the last the last modified date/time of two files.
59
* @param obj1 The first file to compare
60
* @param obj2 The second file to compare
61
* @return a negative value if the first file's lastmodified date/time
62
* is less than the second, zero if the lastmodified date/time are the
63
* same and a positive value if the first files lastmodified date/time
64
* is greater than the second file.
67
public int compare(Object obj1, Object obj2) {
68
File file1 = (File)obj1;
69
File file2 = (File)obj2;
70
long result = file1.lastModified() - file2.lastModified();
73
} else if (result > 0) {