0

I have the following problem: I have two arrays of a different data type, e.g.

byte[] a = {2,5,3};
long[] b = {2,0,1};

And I want b to get sorted and the entries in a must be changed respectively, s.t. I get

byte[] a = {5,3,2};
long[] b = {0,1,2};

I've seen that most people do that with a Comparator but that doesn't seem to work with different data types. I am looking for an efficient solution as the array lengths will be very large. However, a has only two different entries 0 and 1 (but I still want to keep it as byte to have the option for more entries) and all pairs (0,x) are already sorted (that means if I only pick pairs where the first entry is a 0 then it is already sorted). The same holds also for pairs (1,x'), but the entries in b are not completely sorted. I hope my problem got clear. I am happy for any suggestions.

Thank you very much in advance!

Edit: Apparently this question: Sort a parallel array using Arrays.sort() is very similar. However I wasn't able to reproduce any working example. Does anyone have a full working example?

2
  • 2
    Rather than have these "parallel" collections, it would almost certainly be cleaner to create a new class with a byte and a long, then a single collection/array of that type. Then you can just sort that collection/array using a comparator for that type. Commented Nov 16, 2016 at 7:35
  • I've solved it now by going to one data type and using something like this: stackoverflow.com/questions/18232788/… However, I'm not happy with that solution, but it is okay for the moment... Commented Nov 16, 2016 at 11:26

1 Answer 1

0

I would try to solve your problem if you tagged your question , but in your problem description shows, that you're missing a class.

public class MyClass implements Comparable {

    private byte;
    private long;
    //getters setters
    //compare method according to your requirements.
}

Then you'll have MyClass[] which you will be able to sort.

And in case you want to stick to your problem, simply implement a sort algorithm, sort one of your arrays and when only you perform a swap, swap elements in the second array too.

Sign up to request clarification or add additional context in comments.

Comments