How to sort an array with index from another array?

J_DJ_D
edited May 2016 in How To...

Hi all, I have these 2 arrays and I would like to sort the elements of the first with the index of the second. Can you give me a hand?

int[] a = {a,b,c,d,e};
int[] b = {0,3,2,4,1};

I started with sorting the second array, but than I can't sort this problem out.

b = sort(b);

Thanks

Tagged:

Answers

  • Create a class that stores both values, then sort instances of that class according to the sorting value.

  • Hi Kevin, can you show me a simple example of this method?

  • edited August 2015 Answer ✓

    Which part are you confused about?

    Generally, parallel arrays (which is what you have) are a bad idea, mostly because they're hard to work with.

    Let's say we have these parallel arrays:

    String[] names = {"Stanley", "Doctor Meow", "Zeus"};
    int[] ages = {8, 2, 150};
    

    These arrays represent some cats, but they're annoying to work with. Instead, I could create a class that represents a single cat:

    class Cat{
       String name;
       int age;
    
       public Cat(String name, int age){
          this.name = name;
          this.age = age;
       }
    }
    

    Now I can use a single array:

    Cat[] cats = {new Cat("Stanley", 8), new Cat("Doctor Meow", 2), new Cat("Zeus", 150)};

    Or better yet, an ArrayList:

    ArrayList<Cat> cats = new ArrayList<Cat>();
    cats.add(new Cat("Stanley", 8));
    cats.add(new Cat("Doctor Meow", 2));
    cats.add(new Cat("Zeus", 150));
    

    And then if I want to sort them, I can use a custom Comparator or the Comparable interface along with Collections.sort() to do so! That assumes I'm in Java mode, but JavaScript mode is similar.

    I'd suggest doing some googling of those terms and trying something out. Good luck!

  • J_DJ_D
    edited August 2015

    Hi, thanks for the answer..I started following your method but was a bit tricky for me..I'm quite new to processing. So I found another way, and at the moment seems working. I share it here in case someone need it:

        String[] a = {"cat", "dog", "rabbit"};
        float[] b = {3, 5, 1};
    
        String [] sortedA = new String[a.length];
        float [] sortedB = sort(b);
    
        for(int i=0;i<sortedB.length;i++){
          for(int j=0;j<b.length;j++){
            if(sortedB[i]==b[j]){
               sortedA[i]=a[j];
            }
          }
    }
    
  • edited August 2015 Answer ✓

    What KevinWorkman was suggesting was to have a single array where each element stores values of the two arrays. In other words each element would be a class that stores pairs like (a, 0), (b, 3), (c, 2), etc.

    Because each element of the array is like a pair, you would perform the sorting of the array based on one of the two elements of each pair. If each pair (a, 0), (b, 3), (c, 2), etc. has the meaning "animal, number" then the sorting would be based on "number"

    Although your approach works it involves making copies of the arrays which is not very efficient

Sign In or Register to comment.