add sort (ascending) method
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package org.lucares.collections;
|
package org.lucares.collections;
|
||||||
|
|
||||||
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -249,6 +250,13 @@ public class IntList {
|
|||||||
return get(0, index);
|
return get(0, index);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Sorts the list into ascending order.
|
||||||
|
*/
|
||||||
|
public void sort() {
|
||||||
|
Arrays.sort(data, 0, index);
|
||||||
|
}
|
||||||
|
|
||||||
private void ensureCapacity(final int newElements) {
|
private void ensureCapacity(final int newElements) {
|
||||||
|
|
||||||
final int requiredCapacity = index + newElements;
|
final int requiredCapacity = index + newElements;
|
||||||
|
|||||||
@@ -386,6 +386,17 @@ public class IntListTest {
|
|||||||
Assert.assertNotEquals(a.hashCode(), b.hashCode());
|
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) {
|
private int[] removeElements(final int[] data, final int... removedElements) {
|
||||||
final int[] result = new int[data.length - removedElements.length];
|
final int[] result = new int[data.length - removedElements.length];
|
||||||
final List<Integer> blacklist = Arrays.stream(removedElements).boxed().collect(Collectors.toList());
|
final List<Integer> blacklist = Arrays.stream(removedElements).boxed().collect(Collectors.toList());
|
||||||
|
|||||||
Reference in New Issue
Block a user