Java的多组排序 [英] Java Multiple Array Sorting

查看:113
本文介绍了Java的多组排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我现在有4阵列的所有持不同的数据我所遇到的问题是,我要排序的阵列中的一个字母通常我会只是做这样的

 类IgnoreCaseComparator实现比较<串GT; {
   公众诠释比较(STRA字符串,字符串STRB){
      返回strA.compareToIgnoreCase(STRB);
   }
}
IgnoreCaseComparator ICC =新IgnoreCaseComparator();
java.util.Collections.sort(ARRAY,ICC);

如果ARRAY是我想要,但是我需要的数组排序一起按字母顺序排序的数组。

因此​​,例如,可以说我的阵列看起来像这样

 宠物阵:狗,猫,鸟]
业主阵:[吉姆,凯尔,乔]
朋友阵:[杰克,吉姆,约翰]
猫阵列:[假的,真,假]

我要根据宠物阵列我的输出应该是这样的,所有的第三值现在是第一,现在第一个值是第三排序(这是大大超过简化我的实际阵列包含数以千计的投入)

 宠物阵:[鸟,猫,狗]
业主阵:[乔,凯尔,吉姆]
朋友阵:[约翰,吉姆,杰克]
猫阵列:[假的,真,假]

有没有一种简单的方法来做到这一点,我可以整理一个阵列,并有其他人效仿。感谢您对这个问题的任何帮助。

===编辑===

除了我的解决办法是稍有不同,因为我做了一点阅读核对答案是完美的我只有在compareTo方法略有不同的返回行

 实体类实现可比<实体GT; {
        串宠物;
        串所有者;
        串朋友;
        INT猫;        实体(字符串宠物,业主字符串,字符串朋友,诠释猫){
          this.Pet =宠物;
          this.Owner =所有者;
          this.Friend =朋友;
          this.Cat =猫;
        }        @覆盖
        公众诠释的compareTo(实体除外){
            返回this.Pet.compareToIgnoreCase(other.Pet);
        }
     }


解决方案

您可以声明一个对象:

 实体类实现可比<实体GT; {
    宠;
    店主人;
    朋友的朋友;
    布尔猫;    实体(宠,人老板,朋友的朋友,布尔猫){
      this.pet =宠物;
      this.owner =所有者;
      this.friend =的朋友;
      this.cat =猫;
    }    公众诠释的compareTo(实体B){
      返回pet.getName()与compareToIgnoreCase(b.pet.getName())。
    }
 }

然后建立一个数组或这些对象的列表。和排序。

 实体[]数组=新实体[pets.length]
的for(int i = 0; I< pets.length){
    数组[我] =新的实体(宠物[I],所有者[I],朋友[我],猫[I]);
}
Arrays.sort(数组);

之后,你可以回拆分它的阵列。

 的for(int i = 0; I< pets.length){
    宠物[I] =阵列[I] .pet;
    业主[I] =阵列[I] .owner;
    ...
}

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);
        }
     }

解决方案

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;
    ...
}

这篇关于Java的多组排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆