0

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

Dan
2,7292 gold badges31 silver badges34 bronze badges
asked Nov 29, 2013 at 5:42
1

4 Answers 4

3

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;
}
answered Nov 29, 2013 at 5:52
Sign up to request clarification or add additional context in comments.

Comments

2

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]);
 }
 });
answered Nov 29, 2013 at 5:58

Comments

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));
 }
}
answered Nov 29, 2013 at 5:55

Comments

0

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");
 }
}
Elliott Frisch
202k20 gold badges165 silver badges263 bronze badges
answered Nov 29, 2013 at 6:10

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.