Code Pretty Print Script

Thursday, November 21, 2013

A Nicer Way to Sort

When you're dealing with production code, it's nice to have a convenient sort utility method that works with Arrays and Collections.  I think you might find this class useful, especially if you can use import static!

/*
 * Copyright © 2013 - Elliott Frisch
 * 
 * THIS SOFTWARE IS PROVIDED UNDER THE CREATIVE COMMONS
 * LICENSE 3.0 "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER
 * EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR
 * A PARTICULAR PURPOSE.
 * 
 * To use this software you must agree to the complete
 * license terms available at:
 * http://creativecommons.org/licenses/by/3.0/us/deed.en_US
 * 
 * It is the intent of the author(s) that you may use or
 * modify this software for any purpose (including your own
 * commercial gain) provided that this notice remains in its
 * entirety.
 * 
 * Created by Elliott Frisch - www.frischcode.com
 */
package com.frischcode.util;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * <b>Rationale:</b> Provide a simple and convenient
 * mechanism for accessing the various sort(s) for Arrays
 * and Collections.
 * 
 * @author Elliott Frisch
 */
public class SortUtil {
  /**
   * Sort a Collection of Comparable Objects (using a
   * ComparableComparator). After sorting <b>in</b> is also
   * sorted.
   * 
   * @param in
   *          The Collection of Comparable objects.
   * @return The sorted Collection.
   */
  public static <T extends Comparable<T>> Collection<T> sort(
      final Collection<T> in) {
    if (in != null && in.size() > 0) {
      return sort(in, new ComparableComparator<T>());
    }
    return in;
  }

  /**
   * Sort a Collection of Objects using the provided
   * Comparator. After sorting <b>in</b> is also sorted.
   * 
   * @param in
   *          The Collection of Objects to sort.
   * @param comp
   *          The comparator to use.
   * @return The sorted Collection of Objects.
   */
  public static <T> Collection<T> sort(
      final Collection<T> in, Comparator<T> comp) {
    if (in != null && in.size() > 0) {
      if (in instanceof List) {
        Collections.sort((List<T>) in, comp);
      } else {
        List<T> al = new ArrayList<T>(in);
        Collections.sort(al, comp);
        in.clear();
        in.addAll(al);
      }
    }
    return in;
  }

  /**
   * Sort an Array of Comparable Objects (using a
   * ComparableComparator). After sorting <b>in</b> is also
   * sorted.
   * 
   * @param in
   *          The Array of Comparable Objects to sort.
   * @return The sorted Array of Comparable Objects.
   */
  @SafeVarargs
  public static <T extends Comparable<T>> T[] sort(
      final T... in) {
    if (in != null && in.length > 0) {
      return sort(in, new ComparableComparator<T>());
    }
    return in;
  }

  /**
   * Sort any Array of Objects using the provided
   * Comparator. After sorting <b>in</b> is also sorted.
   * 
   * @param in
   *          The Object Array to sort.
   * @param comp
   *          The Comparator to use in sorting.
   * @return The sorted Array of Objects.
   */
  public static <T> T[] sort(final T[] in,
      Comparator<T> comp) {
    if (in != null && in.length > 0) {
      Arrays.sort(in, comp);
    }
    return in;
  }
}

No comments :

Post a Comment