Reputation: 1070
Objects in ArrayList<CustomObject>
array A
are unsorted:
ID: [6, 3, 2, 5, 4, 1]
From another part of a program I received array B
. Need to sort objects in array A
by property int ID
in the way array B
is sorted:
[3, 6, 4, 5, 1, 2]
So array A
should be finally sorted like:
[3, 6, 4, 5, 1, 2]
So the objects in array A
are sorted by ID exactly like items in array B
are.
What would be the best way to implement it?
Upvotes: 1
Views: 567
Reputation: 1575
Approach
I understand that this could be an over kill for a simple solution mentioned above. but I have tried to exploit Comparator
interface from the java.utils
import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
class CustomerCompartor implements Comparator<Customer> {
@SuppressWarnings("rawtypes")
ArrayList arrayOrder;
@SuppressWarnings("unchecked")
public CustomerCompartor(Integer[] arrayOrderInt) {
this.arrayOrder = new ArrayList(Arrays.asList(arrayOrderInt));
}
public int compare(Customer cust1, Customer cust2) {
int cust1Order = this.arrayOrder.indexOf(cust1.getId());
int cust2Order = this.arrayOrder.indexOf(cust2.getId());
return cust1Order - cust2Order;
}
}
class Customer {
int id;
public Customer(int id) {
this.id = id;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
}
public class ArrayOrder {
public static void main(String[] args) {
List<Customer> customerArray = new ArrayList<Customer>();
customerArray.add(new Customer(1));
customerArray.add(new Customer(2));
customerArray.add(new Customer(3));
customerArray.add(new Customer(4));
customerArray.add(new Customer(5));
customerArray.add(new Customer(6));
for (Customer cust : customerArray) {
System.out.println(cust.getId());
}
Integer[] arrayOrder = new Integer[] { 3, 6, 4, 5, 1, 2 };
CustomerCompartor comparator = new CustomerCompartor(arrayOrder);
Collections.sort(customerArray, comparator);
for (Customer cust : customerArray) {
System.out.println(cust.getId());
}
}
}
Upvotes: 0
Reputation:
Try this.
List<CustomObject> a = Arrays.asList(
new CustomObject(6),
new CustomObject(3),
new CustomObject(2),
new CustomObject(5),
new CustomObject(4),
new CustomObject(1)
);
System.out.println(a);
int[] b = {4, 6, 5, 3, 1, 2};
Map<Integer, Integer> map = new HashMap<>();
for (int i = 0; i < b.length; ++i)
map.put(b[i], i);
// for Java8
// Collections.sort(a, (l, r) -> map.get(l.id) - map.get(r.id));
Collections.sort(a, new Comparator<CustomObject>() {
@Override
public int compare(CustomObject l, CustomObject r) {
return map.get(l.id) - map.get(r.id);
}
});
System.out.println(a);
Upvotes: 3
Reputation: 2598
This may helpful to you :
public static void main(String[] args) {
HashMap<Integer, Integer> myMap = new HashMap();
int arrayA[] = {6, 3, 2, 5, 4, 1};
int arrayB[] = {3, 6, 4, 5, 1, 2};
for (int i = 0; i < arrayA.length; i++) {
if (myMap.containsKey(arrayA[i])) {
int pValue = myMap.get(arrayA[i]);
pValue++;
myMap.put(arrayA[i], pValue);
} else if (!myMap.containsKey(arrayA[i])) {
myMap.put(arrayA[i], 1);
}
}
int l = 0;
for (int i = 0; i < arrayB.length; i++) {
if (myMap.containsKey(arrayB[i])) {
int pValue = myMap.get(arrayB[i]);
for (int k = 0; k < pValue; k++) {
arrayA[l++] = arrayB[i];
}
}
}
System.out.println(Arrays.toString(arrayA));
}
Out-put :
[3, 6, 4, 5, 1, 2]
Upvotes: 0