1

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!

Victor Buendía
  • 451
  • 11
  • 21
  • 6
    Why are they in separate data structures? Seems like a `TreeMap` would serve you better if you sort by keys. – C.B. Aug 28 '14 at 15:10
  • why don't you use map and store them as pair? – Adi Aug 28 '14 at 15:10
  • 2
    Any time you're trying to apply the same mutation to multiple data structures, you've got yourself a design smell. Like all the other comments are saying, create a data structure which encompasses all the data elements you need and create a single list of them. – azurefrog Aug 28 '14 at 15:12
  • 2
    possible duplicate of [Can I sort two lists in relation to each other?](http://stackoverflow.com/questions/10213493/can-i-sort-two-lists-in-relation-to-each-other) – Nazarii Bardiuk Aug 28 '14 at 15:14
  • There is no built in option to short like that. U need to use map or go for own program for short both list in same manner. – Janny Aug 28 '14 at 15:16
  • This seems like homework. If not and those are the only data, use `TreeMap` and you're set. If not, start writing your own algorithm. – Luiggi Mendoza Aug 28 '14 at 15:16
  • Not homerwork, I'm coding an AI algorithm for my strategy game. Looks like the `TreeMap` is a good solution, I'm proving it – Victor Buendía Aug 28 '14 at 15:30

5 Answers5

4

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}
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
4

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);
Paul Boddington
  • 37,127
  • 10
  • 65
  • 116
  • At the end I did not use this code, but a pretty similar one, using this concept. Pointed as correct ;D – Victor Buendía Aug 28 '14 at 15:59
  • 2
    Thanks. I should point out that neither of my answers will work if there are any repeated numbers. Because of this problem, the best solution is @Elliott Frisch's one using a List. – Paul Boddington Aug 28 '14 at 23:53
  • Thanks, I will take it in account. However in this case I don't have repeated numbers, I prefer this method -which is easier than creating the `List` – Victor Buendía Aug 29 '14 at 14:13
1

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.

deejay
  • 575
  • 1
  • 8
  • 24
  • I like this, looks simple. But I am doing something bad. Let's see, I'm initializing the `Map` using `map.put(a.get(i), b.get(i))` in a `for` loop. That works good. But I cannot use `Collections.sort()` on the map. I've tried to look the map methods, there's no method returning a list to sort it. – Victor Buendía Aug 28 '14 at 15:34
  • I found something over internet, check [here](http://beginnersbook.com/2013/12/how-to-sort-hashmap-in-java-by-keys-and-values/). I think this is what you r looking for – deejay Aug 28 '14 at 16:27
  • Yep, It's more or less the same Frisch suggested. Thank you! – Victor Buendía Aug 28 '14 at 19:19
0

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.

Community
  • 1
  • 1
djechlin
  • 59,258
  • 35
  • 162
  • 290
0

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.

Simpsons
  • 476
  • 1
  • 7
  • 17