/** * Sorts the given array into sorted order using the given comparator. * * @param self the array to be sorted * @param comparator a Comparator used for the comparison * @return the sorted array * @since 1.5.5 */ public static <T> T[] sort(T[] self, Comparator<? super T> comparator) { return sort(self, true, comparator); }
/** * Sorts the Collection. Assumes that the collection items are comparable * and uses their natural ordering to determine the resulting order. * If the Collection is a List, it is sorted in place and returned. * Otherwise, the elements are first placed into a new list which is then * sorted and returned - leaving the original Collection unchanged. * <pre class="groovyTestCase">assert [1,2,3] == [3,1,2].sort()</pre> * * @param self the Iterable to be sorted * @return the sorted Iterable as a List * @see #sort(Collection, boolean) * @since 2.2.0 */ public static <T> List<T> sort(Iterable<T> self) { return sort(self, true); }
/** * @deprecated Use the Iterable version of sort instead * @see #sort(Iterable,boolean) * @since 1.0 */ @Deprecated public static <T> List<T> sort(Collection<T> self) { return sort((Iterable<T>) self, true); }
/** * @deprecated Use the Iterable version of sort instead * @see #sort(Iterable, boolean, Comparator) * @since 1.8.1 */ @Deprecated public static <T> List<T> sort(Collection<T> self, boolean mutate, Comparator<T> comparator) { return sort((Iterable<T>) self, mutate, comparator); }
/** * @deprecated Use the Iterable version of sort instead * @see #sort(Iterable, boolean, Comparator) * @since 1.0 */ @Deprecated public static <T> List<T> sort(Collection<T> self, Comparator<T> comparator) { return sort((Iterable<T>) self, true, comparator); }
/** * @deprecated Use the Iterable version of sort instead * @see #sort(Iterable, boolean) * @since 1.8.1 */ @Deprecated public static <T> List<T> sort(Collection<T> self, boolean mutate) { return sort((Iterable<T>) self, mutate); }
/** * @deprecated Use the Iterable version of sort instead * @see #sort(Iterable, boolean, Closure) * @since 1.8.1 */ @Deprecated public static <T> List<T> sort(Collection<T> self, boolean mutate, Closure closure) { return sort((Iterable<T>)self, mutate, closure); }
/** * @deprecated Use the Iterable version of sort instead * @see #sort(Iterable, Closure) * @since 1.0 */ @Deprecated public static <T> List<T> sort(Collection<T> self, @ClosureParams(value=FromString.class, options={"T","T,T"}) Closure closure) { return sort((Iterable<T>)self, closure); }
/** * Sorts the given iterator items into a sorted iterator. The items are * assumed to be comparable. The original iterator will become * exhausted of elements after completing this method call. * A new iterator is produced that traverses the items in sorted order. * * @param self the Iterator to be sorted * @return the sorted items as an Iterator * @since 1.5.5 */ public static <T> Iterator<T> sort(Iterator<T> self) { return sort((Iterable<T>) toList(self)).listIterator(); }
/** * Sorts the given iterator items into a sorted iterator using the comparator. The * original iterator will become exhausted of elements after completing this method call. * A new iterator is produced that traverses the items in sorted order. * * @param self the Iterator to be sorted * @param comparator a Comparator used for comparing items * @return the sorted items as an Iterator * @since 1.5.5 */ public static <T> Iterator<T> sort(Iterator<T> self, Comparator<? super T> comparator) { return sort((Iterable<T>) toList(self), true, comparator).listIterator(); }
/** * Sorts the elements from this array into a newly created array using * the Closure to determine the correct ordering. * <p> * If the closure has two parameters it is used like a traditional Comparator. I.e. it should compare * its two parameters for order, returning a negative integer, zero, or a positive integer when the * first parameter is less than, equal to, or greater than the second respectively. Otherwise, * the Closure is assumed to take a single parameter and return a Comparable (typically an Integer) * which is then used for further comparison. * * @param self the array containing the elements to be sorted * @param closure a Closure used to determine the correct ordering * @return the sorted array * @since 1.5.5 */ @SuppressWarnings("unchecked") public static <T> T[] sort(T[] self, @ClosureParams(value=FromString.class, options={"T","T,T"}) Closure closure) { return sort(self, false, closure); }
/** * Sorts this Iterable using the given Closure to determine the correct ordering. If the Iterable is a List, * it is sorted in place and returned. Otherwise, the elements are first placed * into a new list which is then sorted and returned - leaving the original Iterable unchanged. * <p> * If the Closure has two parameters * it is used like a traditional Comparator. I.e. it should compare * its two parameters for order, returning a negative integer, * zero, or a positive integer when the first parameter is less than, * equal to, or greater than the second respectively. Otherwise, * the Closure is assumed to take a single parameter and return a * Comparable (typically an Integer) which is then used for * further comparison. * <pre class="groovyTestCase">assert ["hi","hey","hello"] == ["hello","hi","hey"].sort { it.length() }</pre> * <pre class="groovyTestCase">assert ["hi","hey","hello"] == ["hello","hi","hey"].sort { a, b -> a.length() <=> b.length() }</pre> * * @param self the Iterable to be sorted * @param closure a 1 or 2 arg Closure used to determine the correct ordering * @return a newly created sorted List * @see #sort(Collection, boolean, Closure) * @since 2.2.0 */ public static <T> List<T> sort(Iterable<T> self, @ClosureParams(value=FromString.class, options={"T","T,T"}) Closure closure) { return sort(self, true, closure); }
/** * Sorts the elements from the given map into a new ordered map using * the closure as a comparator to determine the ordering. * The original map is unchanged. * <pre class="groovyTestCase">def map = [a:5, b:3, c:6, d:4].sort { a, b -> a.value <=> b.value } * assert map == [b:3, d:4, a:5, c:6]</pre> * * @param self the original unsorted map * @param closure a Closure used as a comparator * @return the sorted map * @since 1.6.0 */ public static <K, V> Map<K, V> sort(Map<K, V> self, @ClosureParams(value=FromString.class, options={"Map.Entry<K,V>","Map.Entry<K,V>,Map.Entry<K,V>"}) Closure closure) { Map<K, V> result = new LinkedHashMap<K, V>(); List<Map.Entry<K, V>> entries = asList((Iterable<Map.Entry<K, V>>) self.entrySet()); sort((Iterable<Map.Entry<K, V>>) entries, closure); for (Map.Entry<K, V> entry : entries) { result.put(entry.getKey(), entry.getValue()); } return result; }
/** * Sorts the given iterator items into a sorted iterator using the Closure to determine the correct ordering. * The original iterator will be fully processed after the method call. * <p> * If the closure has two parameters it is used like a traditional Comparator. * I.e. it should compare its two parameters for order, returning a negative integer, * zero, or a positive integer when the first parameter is less than, equal to, * or greater than the second respectively. Otherwise, the Closure is assumed * to take a single parameter and return a Comparable (typically an Integer) * which is then used for further comparison. * * @param self the Iterator to be sorted * @param closure a Closure used to determine the correct ordering * @return the sorted items as an Iterator * @since 1.5.5 */ public static <T> Iterator<T> sort(Iterator<T> self, @ClosureParams(value=FromString.class, options={"T","T,T"}) Closure closure) { return sort((Iterable<T>) toList(self), closure).listIterator(); }
T[] answer = (T[]) sort((Iterable<T>) toList(self), closure).toArray(); if (mutate) { System.arraycopy(answer, 0, self, 0, answer.length);
if (sort != null) files = DefaultGroovyMethods.sort((Iterable<File>)files, sort); for (File file : files) { if (file.isDirectory()) {
public void testSortMapKeys() { assertEquals(Arrays.asList("Monkeys", "Lions", "Giraffe"), sort(zoo.keySet(), new Closure<Integer>(null) { public Integer doCall(String a, String b) { return -a.compareTo(b); } })); assertEquals(Arrays.asList("Giraffe", "Lions", "Monkeys"), sort(zoo.keySet(), new Closure<Integer>(null) { public Integer doCall(String a, String b) { return a.compareTo(b); } })); }
if (sort != null) files = DefaultGroovyMethods.sort((Iterable<Path>) files, sort);
public void testGetProperty() throws Exception { MBeanServer mbeanServer = MBeanServerFactory.createMBeanServer(); ObjectName name = new ObjectName("groovy.test:role=TestMBean,type=Dummy"); // use Class.forName instead of new Dummy() to allow separate compilation mbeanServer.registerMBean(Class.forName("groovy.util.Dummy").newInstance(), name); assertEquals("JMX value of Name", "James", mbeanServer.getAttribute(name, "Name")); GroovyMBean object = new GroovyMBean(mbeanServer, name); Object value = object.getProperty("Name"); assertEquals("Name property", "James", value); object.setProperty("Name", "Bob"); assertEquals("Name property", "Bob", object.getProperty("Name")); // now let's look up the name via JMX to check assertEquals("JMX value of Name", "Bob", mbeanServer.getAttribute(name, "Name")); assertEquals("Location : London|Name : Bob|Size : 12", join(sort(object.listAttributeValues()), "|")); assertEquals("start|stop", join(sort(object.listOperationNames()), "|")); assertEquals("void start()", join(sort(object.describeOperation("start")), "|")); assertEquals("(rw) java.lang.String Location", object.describeAttribute("Location")); } }
/** * Sorts the given Object array into a newly created array using the given comparator. * * @param self the array to be sorted * @param closure a Closure used as a comparator * @return the sorted array */ public static Object[] sort(Object[] self, Closure closure) { return sort(toList(self), closure).toArray(); }