您可以使用
Collections.sort(fileView);并实现
Comparable接口并编写
compareTo(fileObjarg0)决定对列表进行排序的实现方法。
为了实施,
class fileObj implements Comparable<fileObj>@Override public int compareTo(fileObj arg0) { if(this.diffSeconds > arg0.diffSeconds ) return 0; else return 1; }
参考:
详细编码。
import java.util.ArrayList;import java.util.Collections;import java.util.Date;import java.util.List;public class SortExample { public static void main(String[] args) { // TODO Auto-generated method stub List<fileObj> fileView = new ArrayList<fileObj>(); for(int j=0; j<10; j++) { fileView.add(new fileObj(j)); } Collections.sort(fileView); for(fileObj obj: fileView){ System.out.println("File " + obj.getDiffSeconds()); } }}class fileObj implements Comparable<fileObj>{ public String fileName; public Date modificationTime; public long diffSeconds; fileObj() { modificationTime = null; } fileObj(long diffSeconds) { modificationTime = null; this.diffSeconds = diffSeconds; } @Override public int compareTo(fileObj arg0) { if(this.diffSeconds > arg0.diffSeconds ) return 0; else return 1; } public long getDiffSeconds() { return diffSeconds; } public void setDiffSeconds(long diffSeconds) { this.diffSeconds = diffSeconds; }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)