Skip to content

Instantly share code, notes, and snippets.

@jenetics
Created November 19, 2019 20:57
Show Gist options
  • Save jenetics/b2063cd55174bded1f00baaa864ffcd5 to your computer and use it in GitHub Desktop.
Save jenetics/b2063cd55174bded1f00baaa864ffcd5 to your computer and use it in GitHub Desktop.
void sortStringArray() {
final String[] strings = new String[]{"g", "f", "r", "u", "a"};
final int[] proxy = ProxySorter.sort(
strings, strings.length,
(a, i, j) -> a[i].compareTo(a[j])
);
// Printing strings in ascending order.
for (int index : proxy) {
System.out.println(strings[index]);
}
}
void sortNodeList() {
final NodeList list = newNodeList();
final int[] proxy = ProxySorter.sort(
list, list.getLength(),
(l, i, j) -> l.item(i).getNodeName().compareTo(l.item(j).getNodeName())
);
// Print the XML nodes, sorted by node name.
for (int index : proxy) {
System.out.println(list.item(index).getNodeName());
}
}
void sortIntFunction() {
final IntFunction<Double> function = i -> Math.sin(i/25.0);
final int[] proxy = ProxySorter.sort(
function, 100,
(f, i, j) -> Double.compare(f.apply(i), f.apply(j))
);
// Printing the function values in ascending order.
for (int index : proxy) {
System.out.println(index + ": " + function.apply(index));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment