[英]Sort List of Objects Based On Another Sorted List Object Positions In Java
I have two list objects like :我有两个列表对象,如:
public class AttributeMaster {
public String attribute_id;
public String view_index;
...
}
List<AttributeMaster> attributes = new ArrayList<AttributeMaster>();
public class AttributeDetail {
public String attribute_id;
public String attribute_name;
...
}
List<AttributeDetail> attribute_detail = new ArrayList<AttributeDetail>();
Here, I need to sort attribute_detail
list based on list attributes
.在这里,我需要根据列表
attributes
对attribute_detail
列表进行排序。 List attribute
is already sorted based on its view_index
property.列表
attribute
已根据其view_index
属性进行排序。 I want to update second list based on index of attribute_master
list.我想根据
attribute_master
列表的索引更新第二个列表。
If one one can help.如果一个人能帮上忙。
Collections.sort(attribute_detail,
Comparator.comparing(item -> attributes.indexOf(item)));
int start_index=0;
for(int i=0;i<attributes.size();i++) {
for(int j=0;j<attribute_detail.size();j++) {
if(attributes.get(i).getAttribute_id().equals(attribute_detail.get(j).getAttribute_id())){
AttributeDetail temp=attribute_detail.get(start_index);
attribute_detail.set(start_index, attribute_detail.get(j));
attribute_detail.set(j,temp);
start_index++;
}
}
}
Through index iterations, the code will check the object existence for both list using attribute_id.通过索引迭代,代码将使用 attribute_id 检查两个列表的对象是否存在。 If the object present in both list, then it will sort the list of attribute_detail based on the index of attributes list.
如果对象同时出现在两个列表中,那么它将根据属性列表的索引对 attribute_detail 列表进行排序。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.