根据另一个列表的排序对ArrayList进行排序 [英] Sorting an ArrayList based on the sorting of another one
问题描述
我在班级(GlobalDataHolder.cardNameAndDescription
)中有一个ArrayList<String>()
,我想按字母顺序对其进行排序,但我也想对ArrayList<Integer>()
(UserBoxGlbImageAdapter.mGLBIcons
),同时对前面提到的arrayList
进行排序.
I have an ArrayList<String>()
in a class (GlobalDataHolder.cardNameAndDescription
) and i want to sort it alphabetically but i also want to sort an ArrayList<Integer>()
(UserBoxGlbImageAdapter.mGLBIcons
) while sorting the previously mentioned arrayList
.
Sidenote :两个数组的大小均为3(0-2).
Sidenote: Both arrays have a size of 3(0-2).
我正在编写自己的自定义 compare()方法来执行此操作,但未实现所需的功能.单击运行排序代码的按钮时,将显示正确的顺序除非我按3次按钮,否则都无法实现,尽管String ArrayList
确实得到了按字母顺序排序.所以我想我只需要对数组进行排序就可以了,数组的大小是数组的大小的3倍.
I am writing my own custom compare() method to do this but i am not achieving what i'm looking for.When i click on the button that runs the sorting code, the correct order doesn't get achieved unless i click the button 3 times although the String ArrayList
does get Alphabetically sorted. So i figured that i just need to sort the arrays as many times as the arrays's size is(so 3 times).
总而言之,String和Integer数据应该以相同的顺序排列,因为它们依赖于另一个数据,但是我无法使它们适用于两个数组.
To sum up, the String and Integer data should be in the same order since they depend on it's other but i can't get that to work for both arrays.
没有一个起作用.有人可以告诉我我在第二数组的排序中做错了什么吗?这是我的代码:
None of that worked. Can someone tell me what i'm doing wrong here with the 2nd array's sorting? Here's my code:
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();
}
}
推荐答案
对索引列表而不是列表本身进行排序并不困难.这样您就可以轻松地对列表进行重新排序.
It is not difficult to sort a list of indexes instead of the list itself. From that you can easily reorder the list.
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");
}
}
打印:
字符串:[1、2、3、4、5、6、7、8、9、10]
The strings: [One, Two, Three, Four, Five, Six, Seven, Eight, Nine, Ten]
如果对它们进行排序,它们将是以下顺序:[7、4、3、8、0、6、5、9、2、1],即[八,五,四,九,一,七,六,十,三,二]
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]
按名称字母顺序排列的数字:[8、5、4、9、1、7、6、10、3、2]
Numbers in alphabetical order of their names: [8, 5, 4, 9, 1, 7, 6, 10, 3, 2]
如果您需要一个自定义比较器,我可以自己决定.
I leave it up to you to add a custom comparator if you feel you need one.
这篇关于根据另一个列表的排序对ArrayList进行排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!