【发布时间】:2018-07-13 03:14:21
【问题描述】:
我在一个班级 (GlobalDataHolder.cardNameAndDescription) 中有一个ArrayList<String>(),我想按字母顺序对其进行排序,但我也想排序一个ArrayList<Integer>() (@ 987654324@) 同时对前面提到的arrayList 进行排序。
旁注:两个数组的大小都是 3(0-2)。
我正在编写自己的自定义 compare() 方法来执行此操作,但我没有实现我想要的。当我单击运行排序代码的按钮时,正确的顺序没有实现除非我点击按钮 3 次,尽管 String ArrayList 确实得到按字母顺序排序。所以我想我只需要将数组排序与数组大小一样多(所以 3 次)。
总而言之,字符串和整数数据的顺序应该相同,因为它们依赖于其他数据,但我无法让它对两个数组都起作用。
这些都不起作用。有人可以告诉我第二个数组的排序我在做什么错吗?这是我的代码:
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();
}
}
【问题讨论】:
-
看起来你的字符串和你的整数以某种方式连接起来。所以你可能想用一个字符串和一个整数变量创建一个自定义类,把那个对象放入一个丢失的对象中,然后按字符串值对其进行排序。 Java 是一种面向对象 编程语言,所以要使用对象!
-
@TimothyTruckle 那些数组列表在我的数据库中,重要的是不要把它们混为一个对象。
-
您需要在数组中的字符串和整数之间建立某种联系之王吗?您可以分两步完成,首先对字符串数组进行排序,然后根据该顺序对整数进行排序。
-
@Dimitri 基本上我已经对字符串列表进行了排序,但是我无法正确排序第二个列表。我想我对索引部分感到困惑,如何正确获取它们并使用它们以正确的顺序交换第二个列表的元素。
标签: java android arrays sorting arraylist