List of usage examples for java.util Comparator Comparator
Comparator
From source file:io.yields.math.framework.property.SortedProperty.java
protected SortedProperty(boolean increasing, Class<T> clazz, String explanation) { this.increasing = increasing; if (clazz.equals(Double.class) || clazz.equals(double.class)) { this.comparator = new Comparator<T>() { @Override/*w ww. j a v a 2s.co m*/ public int compare(T o1, T o2) { return compareDouble(o1, o2); } }; } else { this.comparator = new Comparator<T>() { @Override public int compare(T o1, T o2) { return o1.compareTo(o2); } }; } this.explanation = explanation; }
From source file:com.sporeon.baseutil.OrdenacaoUtil.java
/** * Mtodo responsvel por retorna uma lista de Object ordenada pelo nome do campo informado. * @author Senio Caires/* w ww. j a v a 2s . c o m*/ * @param lista - Lista * @param campo - Campo * @param tipoOrdenacao - Tipo de ordenao * @return lista ordenada */ public static List ordenarLista(final List lista, final String campo, final TipoOrdenacao tipoOrdenacao) { if (lista != null && !lista.isEmpty() && campo != null && !(campo.trim().length() == 0)) { BeanComparator ordem = new BeanComparator(campo, new Comparator() { @Override public int compare(final Object o1, final Object o2) { int resultado = 0; final int maximoCaracteres = 20; if (o1 instanceof Integer) { resultado = ((String) ManipulacaoUtil .adicionarChar('0', maximoCaracteres, o2.toString(), true)) .compareToIgnoreCase((String) ManipulacaoUtil.adicionarChar('0', maximoCaracteres, o1.toString(), true)); } else { resultado = ((String) o2.toString()).compareToIgnoreCase((String) o1.toString()); } return resultado; } }); Collections.sort(lista, ordem); if (TipoOrdenacao.CRESCENTE.equals(tipoOrdenacao)) { Collections.reverse(lista); } } return lista; }
From source file:net.dontdrinkandroot.utils.collections.CollectionUtils.java
public static <T extends Number> double getMean(final Collection<T> collection) { final int size = collection.size(); final List<T> sorted = new ArrayList<T>(collection); Collections.sort(sorted, new Comparator<T>() { @Override/*from w w w . ja v a2 s. co m*/ public int compare(final T n1, final T n2) { return (int) Math.signum(n2.doubleValue() - n1.doubleValue()); } }); if (size % 2 == 0) { return (sorted.get(size / 2).doubleValue() + sorted.get(size / 2 - 1).doubleValue()) / 2; } else { return sorted.get(size / 2).doubleValue(); } }
From source file:de.micromata.tpsb.doc.parser.ParserResult.java
@Override public Iterator<FileInfo> iterator() { Collections.sort(fileInfos, new Comparator<FileInfo>() { @Override/*from www.j a va 2 s.c o m*/ public int compare(FileInfo o1, FileInfo o2) { return o1.getClassName().compareTo(o2.getClassName()); } }); return fileInfos.iterator(); }
From source file:bad.robot.bod.Pair.java
private Pair(Name first, Name second) { assertNotNullAndDifferent(first, second); names.add(first);//from w ww.j a v a2s .c o m names.add(second); sort(names, new Comparator<Name>() { public int compare(Name first, Name second) { return first.compareTo(second.value()); } }); }
From source file:net.sf.click.jquery.examples.services.CustomerService.java
@SuppressWarnings("unchecked") public List<Customer> getCustomersForPage(int offset, int pageSize, final String sortColumn, final boolean ascending) { // Ok this implementation is a cheat since all customers are in memory List<Customer> customers = StartupListener.CUSTOMERS; if (StringUtils.isNotBlank(sortColumn)) { Collections.sort(customers, new Comparator() { public int compare(Object o1, Object o2) { int ascendingSort = ascending ? 1 : -1; Comparable v1 = (Comparable) PropertyUtils.getValue(o1, sortColumn); Comparable v2 = (Comparable) PropertyUtils.getValue(o2, sortColumn); if (v1 == null) { return -1 * ascendingSort; } else if (v2 == null) { return 1 * ascendingSort; }/*from www . j ava 2s . c o m*/ return v1.compareTo(v2) * ascendingSort; } }); } return customers.subList(offset, Math.min(offset + pageSize, customers.size())); }
From source file:ca.travelagency.salesstats.InvoiceItemSales.java
protected InvoiceItemSales(List<InvoiceItem> invoiceItems) { Validate.notNull(invoiceItems);//from w ww . ja v a 2s. c o m this.invoiceItems = invoiceItems; Comparator<InvoiceItem> comparator = new Comparator<InvoiceItem>() { @Override public int compare(InvoiceItem invoiceItem1, InvoiceItem invoiceItem2) { return new CompareToBuilder() .append(invoiceItem1.getInvoice().getDate(), invoiceItem2.getInvoice().getDate()) .append(invoiceItem1.getInvoice().getId(), invoiceItem2.getInvoice().getId()) .append(invoiceItem1.getId(), invoiceItem2.getId()).toComparison(); } }; Collections.sort(this.invoiceItems, comparator); }
From source file:SortUtils.java
/** * Test method/*from w ww .j av a2 s. c o m*/ */ public static void main(String[] args) { Comparator cmp = new Comparator() { public int compare(Object o1, Object o2) { return ((Comparable) o1).compareTo(o2); } }; int n = 1000000; if (args.length == 1) try { n = Integer.parseInt(args[0]); } catch (Exception e) { System.err.println(e); } System.out.println("Generating " + n + " random integers..."); java.util.Random random = new java.util.Random(); Integer[] data = new Integer[n]; for (int i = 0; i < n; i++) { data[i] = new Integer(Math.abs(random.nextInt())); // data[i] = new Integer(i); } int[] indices; long time; System.out.print("Arrays.sort..."); time = System.currentTimeMillis(); Integer[] clone = (Integer[]) data.clone(); Arrays.sort(clone, cmp); System.out.println(System.currentTimeMillis() - time + "ms"); System.out.print("quicksort..."); indices = identity(n); time = System.currentTimeMillis(); sort(indices, data, cmp, false); System.out.println(System.currentTimeMillis() - time + "ms"); for (int i = 1; i < n; i++) if (cmp.compare(data[indices[i - 1]], data[indices[i]]) > 0) System.err.println("proplem: quickSort at " + i); System.out.print("quicksort stable..."); // indices = identity(n); time = System.currentTimeMillis(); sort(indices, data, cmp, true); System.out.println(System.currentTimeMillis() - time + "ms"); for (int i = 1; i < n; i++) { int res = cmp.compare(data[indices[i - 1]], data[indices[i]]); if (res > 0) System.err.println("proplem: quickSort stable at " + i); if (res == 0 && indices[i - 1] > indices[i]) System.err.println("proplem: quickSort stable (not stable) at " + i); } // System.out.print("cheapsort..."); // time = System.currentTimeMillis(); // indices = cheapSort(data, cmp); // System.out.println(System.currentTimeMillis()-time + "ms"); // for (int i = 1; i < n; i++) // if (cmp.compare(data[indices[i-1]], data[indices[i]]) > 0) // System.err.println("proplem: cheapSort at " + i); System.out.print("permutate copy..."); time = System.currentTimeMillis(); Object[] data_copy = permute(inverse(indices), data, true); System.out.println(System.currentTimeMillis() - time + "ms"); for (int i = 1; i < n; i++) if (cmp.compare(data_copy[i - 1], data_copy[i]) > 0) System.err.println("proplem: permute copy at " + i); System.out.print("permutate original..."); time = System.currentTimeMillis(); permute(inverse(indices), data, false); System.out.println(System.currentTimeMillis() - time + "ms"); for (int i = 1; i < n; i++) if (cmp.compare(data[i - 1], data[i]) > 0) System.err.println("proplem: permute original at " + i); }
From source file:it.reply.orchestrator.controller.ControllerTestUtils.java
public static List<Deployment> createDeployments(int total, boolean sorted) { List<Deployment> deployments = Lists.newArrayList(); for (int i = 0; i < total; ++i) { deployments.add(createDeployment()); }/* www . j a va2 s. c o m*/ if (sorted) { deployments.stream().sorted(new Comparator<Deployment>() { @Override public int compare(Deployment o1, Deployment o2) { return o1.getCreated().compareTo(o2.getCreated()); } }).collect(Collectors.toList()); } return deployments; }
From source file:springfox.documentation.spi.service.contexts.Orderings.java
public static Comparator<ApiListingReference> listingPositionComparator() { return new Comparator<ApiListingReference>() { @Override/*from w w w.j av a 2s . c om*/ public int compare(ApiListingReference first, ApiListingReference second) { return Ints.compare(first.getPosition(), second.getPosition()); } }; }