java – 根据另一个的排序对ArrayList进行排序
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 根据另一个的排序对ArrayList进行排序,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含4864字,纯文字阅读大概需要7分钟。
内容图文
![java – 根据另一个的排序对ArrayList进行排序](/upload/InfoBanner/zyjiaocheng/703/d04e13c60f3c470eb15439273cff6ccd.jpg)
我在一个类(GlobalDataHolder.cardNameAndDescription)中有一个ArrayList< String>(),我想按字母顺序排序,但我还想在排序前面提到的arrayList时对ArrayList< Integer>()(UserBoxGlbImageAdapter.mGLBIcons)进行排序.
旁注:两个数组的大小均为3(0-2).
我正在编写自己的自定义compare()方法来执行此操作,但我没有实现我正在寻找的东西.当我单击运行排序代码的按钮时,除非我单击按钮,否则无法实现正确的顺序3次虽然String ArrayList确实按字母排序.所以我认为我只需要对数组进行多次排序,因为数组的大小是(所以3次).
总而言之,String和Integer数据应该是相同的顺序,因为它们依赖于它的另一个但是我不能让它对两个数组都有效.
这些都没有效果.有人可以通过第二个阵列的排序告诉我这里我做错了什么吗?这是我的代码:
public class SortingDialog extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Create a builder to make the dialog building process easier
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Sorting Dialog");
builder.setSingleChoiceItems(R.array.sorting_options, 0,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if (i == 1) {
Toast.makeText(getActivity(), "2nd option Clicked", Toast.LENGTH_SHORT).show();
if (getActivity().getSupportFragmentManager().findFragmentByTag("GLOBAL_FRAGMENT") != null) {
sortGlobalListsBasedOnNameAndDesc();
}
}
for (int j = 0; j < GlobalDataHolder.cardNameAndDescription.size(); j++) {
Log.v("card_names", GlobalDataHolder.cardNameAndDescription.get(j));
}
}
});
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
createToast();
dismiss();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
dismiss();
}
});
return builder.create();
}
private void sortGlobalListsBasedOnNameAndDesc() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
GlobalDataHolder.cardNameAndDescription.sort(new Comparator<String>() {
@Override
public int compare(String s1, String s2) {
int id1 = GlobalDataHolder.cardNameAndDescription.indexOf(s1);
int id2 = GlobalDataHolder.cardNameAndDescription.indexOf(s2);
if (s1.equals(s2)) {
return 0;
} else if (s1.compareToIgnoreCase(s2) > 0) { //s1 is greater
//Collections.swap(UserBoxGlbImageAdapter.mGLBIcons,id2,id1);
swap(UserBoxGlbImageAdapter.mGLBIcons,id2,id1);
swap(GlobalDataHolder.cardNameAndDescription,id2,id1);
Log.d("case1","Called 1 time");
return 1;
} else if (s1.compareToIgnoreCase(s2) < 0) { //s1 is smaller
//Collections.swap(UserBoxGlbImageAdapter.mGLBIcons,id1,id2);
swap(UserBoxGlbImageAdapter.mGLBIcons,id1,id2);
swap(GlobalDataHolder.cardNameAndDescription,id1,id2);
Log.d("case2","Called 1 time");
return -1;
} else {
return 0;
}
}
});
}
}
private void swap(List list,int objIndex1, int objIndex2) {
for (int i=0;i < list.size(); i++) {
Collections.swap(list,objIndex1,objIndex2);
UserBoxGlbImageAdapter.refreshFragmentView(UserBoxGLBFragment.getUserBoxAdapter());
}
}
private void createToast() {
Toast.makeText(getActivity(), "Cards sorted based on AVG Stats", Toast.LENGTH_SHORT).show();
}
}
解决方法:
排序索引列表而不是列表本身并不困难.从那里你可以轻松地重新排序列表.
public class Test {
List<String> testStrings = Arrays.asList(new String[]{"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine", "Ten"});
List<Integer> testNumbers = Arrays.asList(new Integer[]{1, 2, 3, 4, 5, 6, 7, 8, 9, 10});
static <T extends Comparable<T>> List<Integer> getSortOrder(List<T> list) {
// Ints in increasing order from 0. One for each entry in the list.
List<Integer> order = IntStream.rangeClosed(0, list.size() - 1).boxed().collect(Collectors.toList());
Collections.sort(order, new Comparator<Integer>() {
@Override
public int compare(Integer o1, Integer o2) {
// Comparing the contents of the list at the position of the integer.
return list.get(o1).compareTo(list.get(o2));
}
});
return order;
}
static <T> List<T> reorder(List<T> list, List<Integer> order) {
return order.stream().map(i -> list.get(i)).collect(Collectors.toList());
}
public void test() {
System.out.println("The strings: " + testStrings);
List<Integer> sortOrder = getSortOrder(testStrings);
System.out.println("The order they would be if they were sorted: " + sortOrder + " i.e. " + reorder(testStrings, sortOrder));
List<Integer> reordered = reorder(testNumbers, sortOrder);
System.out.println("Numbers in alphabetical order of their names: " + reordered);
}
public static void main(String[] args) {
new Test().test();
System.out.println("Hello");
}
}
打印:
The strings: [One, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten]
The order they would be if they were sorted: [7, 4, 3, 8, 0, 6, 5, 9, 2, 1] i.e. [Eight, Five, Four, Nine, One, Seven, Six, Ten, Three, Two]
Numbers in alphabetical order of their names: [8, 5, 4, 9, 1, 7, 6, 10, 3, 2]
如果您觉得需要,可以随意添加自定义比较器.
内容总结
以上是互联网集市为您收集整理的java – 根据另一个的排序对ArrayList进行排序全部内容,希望文章能够帮你解决java – 根据另一个的排序对ArrayList进行排序所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。