
我在一个类(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@OverrIDepublic 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进行排序所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)