Reputation: 471
I am working in Java. I have two lists, let's call them A and B, which I want to sort.
A is an Integer list, so I have no problem to do that. I simply use Collections.sort()
to obtain a sorted list of integers.
The problem comes with the list B. I want to make the same changes done before in A.. B is a list of objects, but there's no way to associate the changes in B with changes in A. I mean, there's no condition to create a comparator method.
Little example:
I have:
A -> {5,1,3,6,4}
B -> {a,b,c,d,e}
I want to sort A and apply the same changes to B to obtain:
A -> {1,3,4,5,6}
B -> {b,c,e,a,d}
Is there any way to do that using built-in Java functions? I prefer to avoid writing a sorting algorithm myself because of efficiency. Thank you!
Upvotes: 1
Views: 1546
Reputation: 37665
A TreeMap
will always iterate over the keys in the right order, so you can do it like this:
Map<Integer, String> map = new TreeMap<Integer, String>();
map.put(5, "a");
map.put(1, "b");
map.put(3, "c");
map.put(6, "d");
map.put(4, "e");
System.out.println(map.keySet());
System.out.println(map.values());
However if you really want to start and end with the same pair of List
instances, I think you'd have to do something convoluted like this:
List<Integer> numbers = new ArrayList<Integer>(Arrays.asList(5, 1, 3, 6, 4));
List<String> letters = new ArrayList<String>(Arrays.asList("a", "b", "c", "d", "e"));
Map<Integer, String> map = new HashMap<Integer, String>();
for (int i = 0, n = numbers.size(); i < n; i++) {
map.put(numbers.get(i), letters.get(i));
}
Collections.sort(numbers);
letters.clear();
for (int number : numbers) {
letters.add(map.get(number));
}
System.out.println(numbers);
System.out.println(letters);
Upvotes: 4
Reputation: 201537
I would start by creating a POJO to store A
and B
,
static class ABPojo implements Comparable<ABPojo> {
public ABPojo(int a, String b) {
this.a = a;
this.b = b;
}
private int a;
private String b;
public int getA() {
return a;
}
public String getB() {
return b;
}
public int compareTo(ABPojo o) {
if (o instanceof ABPojo) {
ABPojo that = (ABPojo) o;
return Integer.valueOf(a).compareTo(that.getA());
}
return 1;
}
}
Then you can loop over the collection of ABPojo
(s) after sorting to build your output with something like
public static void main(String[] args) {
List<ABPojo> al = new ArrayList<ABPojo>();
al.add(new ABPojo(5, "a"));
al.add(new ABPojo(1, "b"));
al.add(new ABPojo(3, "c"));
al.add(new ABPojo(6, "d"));
al.add(new ABPojo(4, "e"));
Collections.sort(al);
StringBuilder a = new StringBuilder();
StringBuilder b = new StringBuilder();
for (ABPojo pojo : al) {
if (a.length() > 0) {
a.append(",");
} else {
a.append("{");
}
if (b.length() > 0) {
b.append(",");
} else {
b.append("{");
}
a.append(pojo.getA());
b.append(pojo.getB());
}
a.append("}");
b.append("}");
System.out.println("A -> " + a.toString());
System.out.println("B -> " + b.toString());
}
Output is the requested
A -> {1,3,4,5,6}
B -> {b,c,e,a,d}
Upvotes: 4
Reputation: 575
Create a map
with your elements in A as key
and the elements in B as value
resp.
Then Collections.Sort() will automatically sort the A elements and its corresponding B elements.
Upvotes: 1
Reputation: 526
I don't think there is a Java function that will do this.
However, you could use a map structure instead of a list stucture where the key to the data is your int, and the data is the unsortable list.
Upvotes: 0
Reputation: 60848
Start with here:
How to find the permutation of a sort in Java
Then manually apply the permutation.
But it sounds like a design problem; consider one list of classes that implement Comparator<T>
where the comparison function is just on the numbers.
Upvotes: 0