简体   繁体   English

如何对ArrayList中的数组进行排序?

[英]How to sort the arrays in ArrayList?

I have ArrayList it contains so many arrays, each array contain first name, last name. 我有ArrayList,它包含许多数组,每个数组包含名字,姓氏。 now I want to sort the list based on the last name. 现在我想根据姓氏对列表进行排序。

Example: 例:

ArrayList<String[]> list=new ArrayList<String[]>();
 String[] name1={"koti" ,"reddy"};
 String[] name2={"hanu" ,"sanjay"};
 String[] name3={"ajay" ,"zedeja"};
 String[] name4={"basha" ,"kadhar"};

 list.add(name1);
 list.add(name2);
 list.add(name3);
 list.add(name4);

I want the sorting order like: 我想要这样的排序顺序:

basha kadhar 
koti reddy 
hanu sanjay 
ajay zedeja 

Could you please help on this ASAP, Thanks in Advance 请您尽快提供帮助,谢谢

Write a custom Comparator and supply that to the appropriate sort overload along with the data. 编写一个自定义的比较器,并将其与数据一起提供给适当的sort重载。

However, I would recommend a separate Person/Name type, instead of String arrays, as it will make data easier to keep track of and it could implement Comparable (which would eliminate/replace the need of a Comparator). 但是,我建议使用单独的Person/Name类型而不是String数组,因为它可以使数据更容易跟踪, 并且可以实现Comparable (这将消除/替换Comparator的需求)。

Now, when writing an applicable compare/compareTo , the code should look similar to: 现在,在编写适用的compare/compareTo ,代码应类似于:

int cmpLastName = a_lastName.compareTo(b_lastName);
if (cmpLastName == 0) {
    // same lastname, order now based on first name
    return a_firstName.compareTo(b_firstName);
} else {
    // different lastname, so have enough ordering
    return cmpLastName;
}

try this 尝试这个

    Collections.sort(list, new Comparator<String[]>() {
        @Override
        public int compare(String[] o1, String[] o2) {
            int c = o1[0].compareTo(o2[0]);
            if (c != 0) {
                return c;
            }
            return o1[1].compareTo(o2[1]);
        }
    });

This is how I would perform that sort operation. 这就是我执行该排序操作的方式。

public static void main(String[] args) {
  ArrayList<String[]> list = new ArrayList<String[]>();
  String[] name1 = { "koti", "reddy" };
  String[] name2 = { "hanu", "sanjay" };
  String[] name3 = { "ajay", "zedeja" };
  String[] name4 = { "basha", "kadhar" };

  list.add(name1);
  list.add(name2);
  list.add(name3);
  list.add(name4);

  System.out.println("Before sorting");
  for (String[] r : list) {
    System.out.println(Arrays.toString(r));
  }
  Collections.sort(list, new Comparator<String[]>() {
    public int compare(String[] left, String[] right) {
      if (left == null) { // null?
        if (right == null) { // null == null!
          return 0;
        }
        return -1; // null < not(null)
      } else if (right == null) {
        return 1; // not(null) > null.
      }
      // If the last names aren't the same, return the result
      // of comparing the last names.
      if (left[1].compareTo(right[1]) != 0) {
        return left[1].compareTo(right[1]);
      }
      // Return the result of comparing the first names.
      return left[0].compareTo(right[0]);
    }
  });
  System.out.println("After sorting");
  for (String[] r : list) {
    System.out.println(Arrays.toString(r));
  }
}

try this code to achieve your output. 尝试使用此代码来实现输出。

public static void main(String []args){
           ArrayList<String[]> list=new ArrayList<String[]>();
     String[] name1={"koti" ,"reddy"};
     String[] name2={"hanu" ,"sanjay"};
     String[] name3={"ajay" ,"zedeja"};
     String[] name4={"basha" ,"kadhar"};

     list.add(name1);
     list.add(name2);
     list.add(name3);
     list.add(name4);
     Collections.sort(list, new Comparator<String[]>() {
            @Override
            public int compare(String[] s1, String[] s2) {
                int i = s1[0].compareTo(s2[0]);
                if (i != 0) {
                    return i;
                }
                return s1[1].compareTo(s2[1]);
            }
        });
     System.out.println("after sorting"+"\n");
     for (String[] s : list) {
         for(int i=0;i<s.length;i++){ 
         System.out.print(s[i]+"\t");
         }
         System.out.print("\n");
        }



}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM