add sort (ascending) method

This commit is contained in:
2017-02-05 14:18:47 +01:00
parent a535761b46
commit f7870a15a7
2 changed files with 19 additions and 0 deletions

View File

@@ -1,5 +1,6 @@
package org.lucares.collections;
import java.util.Arrays;
import java.util.List;
/**
@@ -249,6 +250,13 @@ public class IntList {
return get(0, index);
}
/**
* Sorts the list into ascending order.
*/
public void sort() {
Arrays.sort(data, 0, index);
}
private void ensureCapacity(final int newElements) {
final int requiredCapacity = index + newElements;

View File

@@ -386,6 +386,17 @@ public class IntListTest {
Assert.assertNotEquals(a.hashCode(), b.hashCode());
}
@Test
public void testSort() {
final IntList list = new IntList();
list.addAll(4, 2, 3, 1);
list.sort();
final int[] expectedE = new int[] { 1, 2, 3, 4 };
Assert.assertArrayEquals(expectedE, list.toArray());
}
private int[] removeElements(final int[] data, final int... removedElements) {
final int[] result = new int[data.length - removedElements.length];
final List<Integer> blacklist = Arrays.stream(removedElements).boxed().collect(Collectors.toList());