Java Multiple Array Sorting

你。 提交于 2019-12-13 16:15:17

问题


Hi I currently have 4 arrays all holding different data the issue i have run into is that i want to sort one of the arrays alphabetically which normally i would just do like this

class IgnoreCaseComparator implements Comparator<String> {
   public int compare(String strA, String strB) {
      return strA.compareToIgnoreCase(strB);
   }
}
IgnoreCaseComparator icc = new IgnoreCaseComparator();
java.util.Collections.sort(ARRAY,icc);

Where ARRAY would be the array I want to sort Alphabetically however I need the arrays to sort together.

So for example lets say my arrays look like this

Pet Array: [Dog, Cat, Bird]
Owner Array: [Jim, Kyle, Joe]
Friend Array: [Jake, Jim, John]
Cat Array: [false, true, false]

And i want to sort based on Pet Array my output should be like this where all of the third values are now the first and the first values are now the third (This is way over simplified my actual arrays contains thousands of inputs)

Pet Array: [Bird, Cat, Dog]
Owner Array: [Joe, Kyle, Jim]
Friend Array: [John, Jim, Jake]
Cat Array: [false, true, false]

Is there an easy way to do this where i can sort one array and have the others follow. Thank you for any help with this issue.

===EDIT===

Other than my solution being slightly different cause i did a little reading the checked answer is perfect i only have a slightly different return line in the compareTo method

    class Entity implements Comparable<Entity> {
        String Pet;
        String Owner;
        String Friend;
        int Cat;

        Entity(String Pet, String Owner, String Friend, int Cat) {
          this.Pet = Pet;
          this.Owner = Owner;
          this.Friend = Friend;
          this.Cat = Cat;
        }

        @Override
        public int compareTo(Entity other) {
            return this.Pet.compareToIgnoreCase(other.Pet);
        }
     }

回答1:


You can declare an object:

class Entity implements Comparable<Entity> {
    Pet pet;
    Person owner;
    Friend friend;
    boolean cat;

    Entity(Pet pet, Person owner, Friend friend, boolean cat) {
      this.pet = pet;
      this.owner = owner;
      this.friend = friend;
      this.cat = cat;
    }

    public int compareTo(Entity b) {
      return pet.getName().compareToIgnoreCase(b.pet.getName());
    }
 }

Then build an array or a list of these objects. And the sort.

Entity[] array = new Entity[pets.length];
for (int i = 0; i < pets.length) {
    array[i] = new Entity(pets[i], owners[i], friends[i], cats[i]);
}
Arrays.sort(array);

After that you can split it back to arrays.

for (int i = 0; i < pets.length) {
    pets[i] = array[i].pet;
    owners[i] = array[i].owner;
    ...
}



回答2:


  1. You can create a class Tuple that holds for fields: pet, owner, friend and cat - with the relevant types for each.
  2. Make this class implement Comparable, which is basically just comparing according to the relevant field. [Or, alternatively - implement a matching Comparator for this class].
  3. Create a Tuple[] combinedArray [or a List<Tuple>], and populate it with your data.
  4. Now, you can just sort combinedArray - and you have the data as you need.
  5. All is left to do - is iterate the sorted array - and spread the data back to the original arrays/lists.



回答3:


Why don't you just make one array containing an object containing the fields Pet, Owner, Friend and Cat and have the comparator sort by the field you want.

class PetContainer { // Find a better name
    public String Pet, Owner, Friend;
    public bool Cat;
}

And then put them in an array and

class IgnoreCaseComparator implements Comparator<PetContainer> {
    public int compare(PetContaier PA, PetContainer PB) {
        return PA.Pet.compareToIgnoreCase(PB.Pet);
    }
}

This is just a rough sketch. Make a get method instead of accessing them by their field and make some decent names for the fields.

Hope it helps.




回答4:


to do this in java you will need to write your own construct to do this.

You are looking for associative array functionality. To get this type of functionality you can use a hashmap or hashtable.

http://docs.oracle.com/javase/1.5.0/docs/api/java/util/HashMap.html

public class CustomeDataType 
{
    private String owner;
    private String friend;
    private Boolean isCat;
    // etc
}

Map<String, CustomDataType > petArray = new HashMap<String, CustomeDataType>();


来源:https://stackoverflow.com/questions/10182462/java-multiple-array-sorting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!