Skip to content

Commit 9364652

Browse files
committed
GROOVY-11653: Create DGM#sort(List) variants that take an IntRange (missing variants)
1 parent 1889601 commit 9364652

1 file changed

Lines changed: 111 additions & 0 deletions

File tree

src/main/java/org/codehaus/groovy/runtime/DefaultGroovyMethods.java

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13470,6 +13470,59 @@ public static <T> List<T> sort(Iterable<T> self, boolean mutate, Comparator<? su
1347013470
return list;
1347113471
}
1347213472

13473+
/**
13474+
* Sorts the Iterable using the given Comparator over the given index range. If the Iterable is a List and mutate
13475+
* is true, it is sorted in place and returned. Otherwise, the elements are first placed
13476+
* into a new list which is then sorted and returned - leaving the original Iterable unchanged.
13477+
* <pre class="groovyTestCase">
13478+
* def orig = ['hello', 'hi', 'Hey', 'a']
13479+
* def sorted = orig.sort(0..2, false, String.CASE_INSENSITIVE_ORDER)
13480+
* assert orig == ['hello', 'hi', 'Hey', 'a']
13481+
* assert sorted == ['hello', 'Hey', 'hi', 'a']
13482+
* </pre>
13483+
*
13484+
* @param self the Iterable to be sorted
13485+
* @param range the inclusive range of index values over which to sort
13486+
* @param mutate false causes a new list to be created, true will mutate lists in place
13487+
* @param comparator a Comparator used for the comparison
13488+
* @return a sorted List
13489+
* @since 5.0.0
13490+
*/
13491+
public static <T> List<T> sort(List<T> self, IntRange range, boolean mutate, Comparator<? super T> comparator) {
13492+
Objects.requireNonNull(self);
13493+
RangeInfo info = range.subListBorders(self.size());
13494+
Objects.checkFromToIndex(info.from, info.to, self.size());
13495+
T[] a = (T[]) self.toArray();
13496+
Arrays.sort(a, info.from, info.to, comparator);
13497+
if (!mutate) {
13498+
return Arrays.asList(a);
13499+
}
13500+
ListIterator<T> i = self.listIterator();
13501+
for (T e : a) {
13502+
i.next();
13503+
i.set(e);
13504+
}
13505+
return self;
13506+
}
13507+
13508+
/**
13509+
* Sorts the Iterable using the given Comparator over the given index range.
13510+
* <pre class="groovyTestCase">
13511+
* def orig = ['hello', 'hi', 'Hey', 'a']
13512+
* orig.sort(0..2, String.CASE_INSENSITIVE_ORDER)
13513+
* assert orig == ['hello', 'Hey', 'hi', 'a']
13514+
* </pre>
13515+
*
13516+
* @param self the Iterable to be sorted
13517+
* @param range the inclusive range of index values over which to sort
13518+
* @param comparator a Comparator used for the comparison
13519+
* @return a sorted List
13520+
* @since 5.0.0
13521+
*/
13522+
public static <T> List<T> sort(List<T> self, IntRange range, Comparator<? super T> comparator) {
13523+
return sort(self, range, true, comparator);
13524+
}
13525+
1347313526
/**
1347413527
* Sorts elements in the given index range using the given Closure to determine the ordering.
1347513528
* If mutate is true, it is sorted in place and returned. Otherwise, the elements are first placed
@@ -13543,6 +13596,22 @@ public static <T> List<T> sort(List<T> self, IntRange range, @ClosureParams(valu
1354313596
return sort(self, range, true, closure);
1354413597
}
1354513598

13599+
/**
13600+
* A mutating sort variant that takes an index range.
13601+
* <pre class="groovyTestCase">
13602+
* def nums = [5, 9, 1, 7, 3, 4, 8, 6, 0, 2]
13603+
* nums.sort(0..4)
13604+
* assert nums == [1, 3, 5, 7, 9, 4, 8, 6, 0, 2]
13605+
* </pre>
13606+
*
13607+
* @see #sort(List, IntRange, boolean, Closure)
13608+
* @return the sorted list
13609+
* @since 5.0.0
13610+
*/
13611+
public static <T> List<T> sort(List<T> self, IntRange range) {
13612+
return sort(self, range, true, new NumberAwareComparator<>());
13613+
}
13614+
1354613615
/**
1354713616
* Sorts the given iterator items into a sorted iterator using the Closure to determine the correct ordering.
1354813617
* The original iterator will be fully processed after the method call.
@@ -15311,6 +15380,9 @@ public static <T> Iterator<T> toSorted(Iterator<T> self, @ClosureParams(value=Fr
1531115380
* assert nums.toSorted(0..4) { it } == [1, 3, 5, 7, 9, 4, 8, 6, 0, 2]
1531215381
* </pre>
1531315382
*
15383+
* @param self the Iterable to be sorted
15384+
* @param range the inclusive range of index values over which to sort
15385+
* @param closure a Closure used for the comparison
1531415386
* @see #sort(List, IntRange, boolean, Closure)
1531515387
* @return the sorted list
1531615388
* @since 5.0.0
@@ -15319,6 +15391,45 @@ public static <T> List<T> toSorted(List<T> self, IntRange range, @ClosureParams(
1531915391
return sort(self, range, false, closure);
1532015392
}
1532115393

15394+
/**
15395+
* Returns a list, sorted over the given range
15396+
* using a {@link NumberAwareComparator}, leaving the original list unmodified.
15397+
* <pre class="groovyTestCase">
15398+
* def nums = [5, 9, 1, 7, 3, 4, 8, 6, 0, 2]
15399+
* assert nums.toSorted(0..4, Comparator.reverseOrder()) == [9, 7, 5, 3, 1, 4, 8, 6, 0, 2]
15400+
* assert nums.toSorted(5..-1, Comparator.reverseOrder()) == [5, 9, 1, 7, 3, 8, 6, 4, 2, 0]
15401+
* </pre>
15402+
*
15403+
* @param self the List to be sorted
15404+
* @param range the inclusive range of index values over which to sort
15405+
* @param comparator a Comparator used for the comparison
15406+
* @see #sort(List, IntRange, boolean, Comparator)
15407+
* @return the sorted list
15408+
* @since 5.0.0
15409+
*/
15410+
public static <T> List<T> toSorted(List<T> self, IntRange range, Comparator<? super T> comparator) {
15411+
return sort(self, range, false, comparator);
15412+
}
15413+
15414+
/**
15415+
* Returns a list, sorted over the given range
15416+
* using a {@link NumberAwareComparator}, leaving the original list unmodified.
15417+
* <pre class="groovyTestCase">
15418+
* def nums = [5, 9, 1, 7, 3, 4, 8, 6, 0, 2]
15419+
* assert nums.toSorted(0..4) == [1, 3, 5, 7, 9, 4, 8, 6, 0, 2]
15420+
* assert nums.toSorted(5..-1) == [5, 9, 1, 7, 3, 0, 2, 4, 6, 8]
15421+
* </pre>
15422+
*
15423+
* @param self the List to be sorted
15424+
* @param range the inclusive range of index values over which to sort
15425+
* @see #toSorted(List, IntRange, Closure)
15426+
* @return the sorted list
15427+
* @since 5.0.0
15428+
*/
15429+
public static <T> List<T> toSorted(List<T> self, IntRange range) {
15430+
return toSorted(self, range, new NumberAwareComparator<>());
15431+
}
15432+
1532215433
/**
1532315434
* Sorts the elements from the given map into a new ordered map using
1532415435
* a {@link NumberAwareComparator} on map entry values to determine the resulting order.

0 commit comments

Comments
 (0)