简体   繁体   中英

How to sort the arrays in ArrayList?

I have ArrayList it contains so many arrays, each array contain first name, last name. 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.

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).

Now, when writing an applicable compare/compareTo , the code should look similar to:

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



}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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