Java tutorial
/* * The MIT License (MIT) * * Copyright (c) 2013-2014 Jeff Nelson, Cinchapi Software Collective * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ package org.cinchapi.concourse.util; import java.io.File; import java.util.Comparator; import com.google.common.primitives.Longs; /** * A {@link Comparator} that sorts files with strictly numerical names between 0 * and 2^63 - 1 (i.e. a timestamp). * * @author jnelson */ public enum NaturalSorter implements Comparator<File> { INSTANCE; @Override public int compare(File f1, File f2) { String[] s1 = f1.getName().split("\\."); String[] s2 = f2.getName().split("\\."); int result; return (result = Longs.compare(Long.parseLong(s1[0]), Long.parseLong(s2[0]))) == 0 ? s1[1].compareTo(s2[1]) : result; } }