Sorting an ArrayList of objects using a custom sorting order

后端 未结 11 1454
说谎
说谎 2020-11-22 00:14

I am looking to implement a sort feature for my address book application.

I want to sort an ArrayList contactArray. Contact

11条回答
  •  南笙
    南笙 (楼主)
    2020-11-22 00:54

    I did it by the following way. number and name are two arraylist. I have to sort name .If any change happen to name arralist order then the number arraylist also change its order.

    public void sortval(){
    
            String tempname="",tempnum="";
    
             if (name.size()>1) // check if the number of orders is larger than 1
                {
                    for (int x=0; x 0)
                            {
    
                                tempname = name.get(i);
    
                                tempnum=number.get(i);
    
    
                               name.set(i,name.get(i+1) );
                               name.set(i+1, tempname);
    
                                number.set(i,number.get(i+1) );
                                number.set(i+1, tempnum);
    
    
                            }
                        }
                    }
                }
    
    
    
    }
    

提交回复
热议问题