### Question:
CollectionUtils { public static <T> Collection<T> sortCollection(Collection<T> collection, Comparator<T> comparator) { if (collection instanceof List) { ((List<T>) collection).sort(comparator); return collection; } else if (collection instanceof Set) { TreeSet<T> ret = new TreeSet<T>(comparator); ret.addAll(collection); return ret; } else { return collection; } }  static LinkedList<Object> toLinkedList(Object[] array); static boolean contains(Collection collection, Class clazz); static Object getFirstMatch(Object[] collection, Class clazz); static Object getFirstMatch(List collection, Class clazz); static Map<Object, Collection> classify(Collection<T> srcCollection, ClassifyFilter classifyFilter); static T constructCollectionByType(Class collectionType); static Collection<T> sortCollection(Collection<T> collection, Comparator<T> comparator);  }### Answer:
@Test public void testSortListCollection() { List<TestBean> list = new ArrayList<TestBean>() { { add(new TestBean("b", "b3")); add(new TestBean("b", "b1")); add(new TestBean("b", "b2")); add(new TestBean("a", "a2")); add(new TestBean("a", "a1")); add(new TestBean("a", "a3")); } }; Collection<TestBean> testBeans = CollectionUtils.sortCollection(list, Comparator.comparing(TestBean::getValue)); for (TestBean testBean : testBeans) { System.out.println(testBean); } }
@Test public void testSortHashSetCollection() { Set<TestBean> list = new HashSet<TestBean>() { { add(new TestBean("b", "b3")); add(new TestBean("b", "b1")); add(new TestBean("b", "b2")); add(new TestBean("a", "a2")); add(new TestBean("a", "a1")); add(new TestBean("a", "a3")); } }; Collection<TestBean> testBeans = CollectionUtils.sortCollection(list, Comparator.comparing(TestBean::getValue)); for (TestBean testBean : testBeans) { System.out.println(testBean); } }
@Test public void testSortTreeSetCollection() { Set<TestBean> list = new TreeSet<TestBean>(Comparator.comparing(TestBean::getValue)) { { add(new TestBean("b", "b3")); add(new TestBean("b", "b1")); add(new TestBean("b", "b2")); add(new TestBean("a", "a2")); add(new TestBean("a", "a1")); add(new TestBean("a", "a3")); } }; Collection<TestBean> testBeans = CollectionUtils.sortCollection(list, Comparator.comparing(TestBean::getValue)); for (TestBean testBean : testBeans) { System.out.println(testBean); } }