Java tutorial
//package com.java2s; /* * Copyright JTheque (Baptiste Wicht) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.concurrent.CopyOnWriteArrayList; public class Main { /** * Sort the CopyOnWriteArrayList. * * @param list The list to sort. * @param <E> The type of data. */ public static <E extends Comparable<E>> void sort(CopyOnWriteArrayList<E> list) { Object[] content = list.toArray(); Arrays.sort(content); for (int i = 0; i < content.length; i++) { list.set(i, (E) content[i]); } } /** * Sort the CopyOnWriteArrayList using the given comparator. * * @param list The list to sort. * @param comparator The comparator to use. * @param <E> The type of data. */ public static <E> void sort(CopyOnWriteArrayList<E> list, Comparator<E> comparator) { Object[] content = list.toArray(); Arrays.sort(content, (Comparator) comparator); for (int i = 0; i < content.length; i++) { list.set(i, (E) content[i]); } } /** * Sort a list. * * @param list The list to sort. * @param comparator The comparator to use to sort the list. * @param <T> The type of object in the collection. */ public static <T> void sort(List<T> list, Comparator<T> comparator) { Collections.sort(list, comparator); } }