0

I have an ArrayList(list A) containing some ids and another ArrayList(list B) containing names.I have sorted list A by

Collections.sort(list A);

I need to obtain list B according to their corresponding ids.What am I supposed to do in this case?

4
  • 2
    use TreeMap or HashMap instead of Arraylist it provide facility as you want Commented Jun 16, 2017 at 4:41
  • Redesign it. Have one class that has names and ids, and make an ArrayList of those. Commented Jun 16, 2017 at 4:42
  • doesn't make any sense - how is the second list linked to first?. You should hold both in one structure/class and sort on id/name as you want. Implement Comparable/Comparator for sorting. Commented Jun 16, 2017 at 4:57
  • While using Treemap I got the output.Thank you Pritesh vadhiya Commented Jun 16, 2017 at 6:33

3 Answers 3

2

No need of a new List to sort. Since they are mapped, they can be used directly.

public class A {

public static void main(String[] args) {

List<Integer> idList = new ArrayList<Integer>();
idList.add(5);
idList.add(2);
idList.add(4);
idList.add(1);
idList.add(3);

List<String> nameList = new ArrayList<String>();
nameList.add("Priya");
nameList.add("Pooja");
nameList.add("Sneha");
nameList.add("Ritesh");
nameList.add("Nikita");

Map<Integer, String> map = new HashMap<Integer, String>();
for(int i = 0; i < idList.size(); i++) {
  map.put(idList.get(i), nameList.get(i));   
}


Collection.sort(idList);
nameList.clear();

for(int i = 0; i < map.size(); i++) {
  nameList.add(map.get(idList.get(i)));
}



}
Sign up to request clarification or add additional context in comments.

1 Comment

Code indentation is broken, you can fix it by editing your answer..
1

One possible way of accomplish this is :

Create a class and add the needed properties to it. Then use Comparator to sort according to need.

I have given example of how to sort on id and name basis of class Student

public class Student {
    private int id;
    private String name;

    public Student(int id, String name) {
        this.id=id;
        this.name=name;
    }
    @Override
    public String toString() {
        return this.id+"-"+this.name;
    }

    public String getName(){
        return this.name;
    }

    public static void main(String[] args) {
        List<Student> stu = new ArrayList<>();
        stu.add(new Student(4,"Alaska"));
        stu.add(new Student(5,"Barlin"));
        stu.add(new Student(9,"America"));
        stu.add(new Student(2,"India"));
        Comparator<Student> byId = (Student s1,Student s2)->(s1.id)-(s2.id); //java 8 lambda expression
        stu.sort(byId);
        System.out.println(stu);
        //Way-1 -java 8 lambda expression
        Comparator<Student> byName = (Student s1,Student s2)->s1.getName().compareTo(s2.getName());
         //Way-2 - java 8 method reference 
        //Comparator<Student> byName = Comparator.comparing(Student::getName);
        stu.sort(byName);
        System.out.println(stu);

    }
}

[2-India, 4-Alaska, 5-Barlin, 9-America]

[4-Alaska, 9-America, 5-Barlin, 2-India]

Comments

0
 public class A {

 public static void main(String[] args) {

   List<Integer> idList = new ArrayList<Integer>();
   idList.add(5);
   idList.add(2);
   idList.add(4);
   idList.add(1);
   idList.add(3);

   List<String> nameList = new ArrayList<String>();
   nameList.add("Priya");
   nameList.add("Pooja");
   nameList.add("Sneha");
   nameList.add("Ritesh");
   nameList.add("Nikita");

   Map<Integer, String> map = new HashMap<Integer, String>();
   for(int i = 0; i < idList.size(); i++) {
      map.put(idList.get(i), nameList.get(i));   
   }

   List<Integer> list = new ArrayList<Integer>(map.keySet());
   Collection.sort(list);
   idList.clear();
   nameList.clear();

   for(int i = 0; i < map.size(); i++) {
      idList.add(list.get(i));
      nameList.add(map.get(list.get(i)));
   }


 }

}

Comments

Your Answer

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.