1

I have two array lists list1 and list2 and I want list2 order as the order of list1. Is there any way to do this? In one list I got the top performing emploee from database and list two got from database second time using the top performing employee ids using "in" clause.


List<Long> list1 = new ArrayList<Long>();
list1.add(5645);
list1.add(2312);
list1.add(7845);
list1.add(1212);

and other List of object type:

List<Employee> list2 = new ArrayList<Employee>();
list2.add(new Employee(1212, "A"));
list2.add(new Employee(2312, "V"));
list2.add(new Employee(5645, "D"));
list2.add(new Employee(7845, "T"));

where list1 shows the employee id of top 4;

and I got the employee detail from the data base using id and got this list2.

Now I want to make the order of list2 as list1 to show on the html page.

laune
  • 31,114
  • 3
  • 29
  • 42

3 Answers3

0
List<Long> list1 = new ArrayList<Long>();
list1.add(5645);
list1.add(2312);
list1.add(7845);
list1.add(1212);

and other List of object type:

List<Employee> list2 = new ArrayList<Employee>();
list2.add(new Employee(1212, "A"));
list2.add(new Employee(2312, "V"));
list2.add(new Employee(5645, "D"));
list2.add(new Employee(7845, "T"));    

    Collections.sort(list1, new Comparator<Long>() {

        @Override
        public int compare(Parameter) {
            //Add your logic here for sorting
        }

    });
    // now sort the list B according to the changes made with the order of
    // items in list1
    Collections.sort(list2, new Comparator<Employee>() {

        @Override
        public int compare(Parameter) {
          //                your logic for sorting.
        }

    });

May be this link help you : In Java how do you sort one list based on another?

Community
  • 1
  • 1
Shiladittya Chakraborty
  • 4,270
  • 8
  • 45
  • 94
0

Just iterate list1 and for each item find the matching element in list2. No need to sort.

List<Employee> sortedList = new ArrayList<>();
for (Long id : list1) {
    Employee match = CollectionUtils.find(list2, e -> e.id.equals(id));
    // cannot sort, if list2 does not contain the id, or put your rule for this case
    assert match != null; 
    sortedList.add(match);
}

This uses Apache Commons Collections's CollectionUtils.

Or, for better performance, build a Map first:

Map<Long, Employee> lookupMap = new HashMap<>(list2.size());
for (Employee e : list2)
    lookupMap.put(e.id, e);

List<Employee> sortedList = new ArrayList<>(list2.size());
for (Long id : list1)
    sortedList.add(lookupMap.get(id));
Oliv
  • 10,221
  • 3
  • 55
  • 76
0

What you should do is to let the database order the results:

SELECT name, score FROM Employees
  ORDER BY score DESC
  LIMIT 4;

If you insist on sorting in Java, use a custom Comparator:

List<Long> top = Arrays.asList(5645L, 2312L, 7845L, 1212L);
List<Employee> employees = Arrays.asList(
        new Employee(1212, "A"),
        new Employee(2312, "V"),
        new Employee(5645, "D"),
        new Employee(7845, "T")
);

Comparator<Employee> indexOf = (o1, o2) -> top.indexOf(o1.id) - top.indexOf(o2.id);
Collections.sort(employees, indexOf);
folkol
  • 4,752
  • 22
  • 25