...
 
Commits (3)
.classpath
.idea
.project
.settings
out
target
**/gitignore
*.iml
*.class
*.bin
src/eu/happycoders/sorting/tmp/
package eu.happycoders.sorting;
import java.util.Comparator;
public class Customer implements Comparable<Customer> {
public static final Comparator<Customer> NAME_COMPARATOR =
new Comparator<>() {
@Override
public int compare(Customer o1, Customer o2) {
int result = o1.getLastName().compareTo(o2.getLastName());
if (result != 0) return result;
result = o1.getFirstName().compareTo(o2.getFirstName());
if (result != 0) return result;
return Integer.compare(o1.getId(), o2.getId());
}
};
private final int id;
private final String firstName;
private final String lastName;
public Customer(int id, String firstName, String lastName) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
}
public int getId() {
return id;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
@Override
public int compareTo(Customer o) {
return Integer.compare(this.id, o.id);
}
@Override
public String toString() {
return "Customer{" +
"id=" + id +
", firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
'}';
}
}
package eu.happycoders.sorting;
import java.util.Arrays;
public class CustomerArraySortByIdDescDemo {
public static void main(String[] args) {
Customer[] customers = {
new Customer(43423, "Elizabeth", "Mann"),
new Customer(10503, "Phil", "Gruber"),
new Customer(61157, "Patrick", "Sonnenberg"),
new Customer(28378, "Marina", "Metz"),
new Customer(57299, "Caroline", "Albers")
};
Arrays.sort(customers, new CustomerByIdComparator(false));
System.out.println(Arrays.toString(customers));
}
}
package eu.happycoders.sorting;
import java.util.*;
public class CustomerArraySortByNameComparingDemo {
public static void main(String[] args) {
Customer[] customers = {
new Customer(43423, "Elizabeth", "Mann"),
new Customer(10503, "Phil", "Gruber"),
new Customer(61157, "Patrick", "Sonnenberg"),
new Customer(28378, "Marina", "Metz"),
new Customer(57299, "Caroline", "Albers")
};
Arrays.sort(customers,
Comparator.comparing(Customer::getLastName)
.thenComparing(Customer::getFirstName)
.thenComparingInt(Customer::getId));
System.out.println(Arrays.toString(customers));
}
}
package eu.happycoders.sorting;
import java.util.Arrays;
public class CustomerArraySortByNameDemo {
public static void main(String[] args) {
Customer[] customers = {
new Customer(43423, "Elizabeth", "Mann"),
new Customer(10503, "Phil", "Gruber"),
new Customer(61157, "Patrick", "Sonnenberg"),
new Customer(28378, "Marina", "Metz"),
new Customer(57299, "Caroline", "Albers")
};
Arrays.sort(customers, Customer.NAME_COMPARATOR);
System.out.println(Arrays.toString(customers));
}
}
package eu.happycoders.sorting;
import java.util.Arrays;
public class CustomerArraySortByNameLambdaDemo {
public static void main(String[] args) {
Customer[] customers = {
new Customer(43423, "Elizabeth", "Mann"),
new Customer(10503, "Phil", "Gruber"),
new Customer(61157, "Patrick", "Sonnenberg"),
new Customer(28378, "Marina", "Metz"),
new Customer(57299, "Caroline", "Albers")
};
Arrays.sort(customers,
(o1, o2) -> o1.getLastName().compareTo(o2.getLastName()));
System.out.println(Arrays.toString(customers));
}
}
package eu.happycoders.sorting;
import java.util.Arrays;
public class CustomerArraySortDemo {
public static void main(String[] args) {
Customer[] customers = {
new Customer(43423, "Elizabeth", "Mann"),
new Customer(10503, "Phil", "Gruber"),
new Customer(61157, "Patrick", "Sonnenberg"),
new Customer(28378, "Marina", "Metz"),
new Customer(57299, "Caroline", "Albers")
};
Arrays.sort(customers);
System.out.println(Arrays.toString(customers));
}
}
package eu.happycoders.sorting;
import java.util.Comparator;
public class CustomerByIdComparator implements Comparator<Customer> {
private final boolean ascending;
public CustomerByIdComparator(boolean ascending) {
this.ascending = ascending;
}
@Override
public int compare(Customer o1, Customer o2) {
int result = Integer.compare(o1.getId(), o2.getId());
return ascending ? result : -result;
}
}
package eu.happycoders.sorting;
import java.util.*;
public class CustomerListCollectionsSortByNameDemo {
public static void main(String[] args) {
ArrayList<Customer> customers = new ArrayList<>(List.of(
new Customer(43423, "Elizabeth", "Mann"),
new Customer(10503, "Phil", "Gruber"),
new Customer(61157, "Patrick", "Sonnenberg"),
new Customer(28378, "Marina", "Metz"),
new Customer(57299, "Caroline", "Albers")
));
Collections.sort(customers, Customer.NAME_COMPARATOR);
System.out.println(customers);
}
}
package eu.happycoders.sorting;
import java.util.*;
public class CustomerListCollectionsSortDemo {
public static void main(String[] args) {
ArrayList<Customer> customers = new ArrayList<>(List.of(
new Customer(43423, "Elizabeth", "Mann"),
new Customer(10503, "Phil", "Gruber"),
new Customer(61157, "Patrick", "Sonnenberg"),
new Customer(28378, "Marina", "Metz"),
new Customer(57299, "Caroline", "Albers")
));
Collections.sort(customers);
System.out.println(customers);
}
}
package eu.happycoders.sorting;
import java.util.*;
public class CustomerListSortByNameDemo {
public static void main(String[] args) {
ArrayList<Customer> customers = new ArrayList<>(List.of(
new Customer(43423, "Elizabeth", "Mann"),
new Customer(10503, "Phil", "Gruber"),
new Customer(61157, "Patrick", "Sonnenberg"),
new Customer(28378, "Marina", "Metz"),
new Customer(57299, "Caroline", "Albers")
));
customers.sort(Customer.NAME_COMPARATOR);
System.out.println(customers);
}
}
package eu.happycoders.sorting;
import java.util.*;
public class CustomerListSortDemo {
public static void main(String[] args) {
ArrayList<Customer> customers = new ArrayList<>(List.of(
new Customer(43423, "Elizabeth", "Mann"),
new Customer(10503, "Phil", "Gruber"),
new Customer(61157, "Patrick", "Sonnenberg"),
new Customer(28378, "Marina", "Metz"),
new Customer(57299, "Caroline", "Albers")
));
customers.sort(null);
System.out.println(customers);
}
}
package eu.happycoders.sorting;
import java.util.Arrays;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Consumer;
public class DoubleArrayParallelSortDemo {
private static final int NUMBER_OF_ELEMENTS = 100_000_000;
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
sortTest("sort", Arrays::sort);
sortTest("parallelSort", Arrays::parallelSort);
}
}
private static void sortTest(String methodName,
Consumer<double[]> sortMethod) {
double[] a = createRandomArray(NUMBER_OF_ELEMENTS);
long time = System.currentTimeMillis();
sortMethod.accept(a);
time = System.currentTimeMillis() - time;
System.out.println(methodName + "() took " + time + " ms");
}
private static double[] createRandomArray(int n) {
ThreadLocalRandom current = ThreadLocalRandom.current();
double[] a = new double[n];
for (int i = 0; i < n; i++) {
a[i] = current.nextDouble();
}
return a;
}
}
package eu.happycoders.sorting;
import java.util.Arrays;
public class IntArrayRangeSortDemo {
public static void main(String[] args) {
int[] a = {4, 8, 5, 9, 2, 3, 1, 7, 6};
Arrays.sort(a, 0, 5);
System.out.println(Arrays.toString(a));
}
}
package eu.happycoders.sorting;
import java.util.Arrays;
public class IntArraySortDemo {
public static void main(String[] args) {
int[] a = {4, 8, 5, 9, 2, 3, 1, 7, 6};
Arrays.sort(a);
System.out.println(Arrays.toString(a));
}
}
package eu.happycoders.sorting;
import java.util.Arrays;
public class IntegerArraySortDemo {
public static void main(String[] args) {
Integer[] a = {4, 8, 5, 9, 2, 3, 1, 7, 6};
Arrays.sort(a);
System.out.println(Arrays.toString(a));
}
}
package eu.happycoders.sorting;
import java.util.Arrays;
public class StringArraySortDemo {
public static void main(String[] args) {
String[] names = {"Susan", "Thomas", "Judith", "Daniel", "Eva", "Ben",
"Antonia", "Paul"};
Arrays.sort(names);
System.out.println(Arrays.toString(names));
}
}