Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何通过单击按钮按字母顺序对数组进行双向排序_Java_Android - Fatal编程技术网

Java 如何通过单击按钮按字母顺序对数组进行双向排序

Java 如何通过单击按钮按字母顺序对数组进行双向排序,java,android,Java,Android,我有一个包含国家列表的数组。 我有一个按钮,当我点击它时,它可以按abc排序数组。 我希望当我再次单击同一按钮时,它将按cba对数组进行排序。 升序和降序 下面是对数组进行排序的方法 public void sortByNativeName() { Collections.sort(mCountries, new Comparator<Country>() { @Override public int compare(C

我有一个包含国家列表的数组。 我有一个按钮,当我点击它时,它可以按abc排序数组。 我希望当我再次单击同一按钮时,它将按cba对数组进行排序。 升序和降序

下面是对数组进行排序的方法

public void sortByNativeName() {
        Collections.sort(mCountries, new Comparator<Country>() {
            @Override
            public int compare(Country o1, Country o2) {
                return o1.getNativeName().compareToIgnoreCase(o2.getNativeName());
            }
        });
        updateAdapter(mCountries);
    }
public void sortByNativeName(){
Collections.sort(mccountries,newcomparator(){
@凌驾
公共整数比较(国家o1,国家o2){
返回o1.getNativeName().CompareTIgnoreCase(o2.getNativeName());
}
});
UpdateDapter(McCountries);
}
您可以使用以下方法:

public void sortByNativeName() {
        Collections.sort(mCountries, new Comparator<Country>() {
            @Override
            public int compare(Country o1, Country o2) {
                return o1.getNativeName().compareToIgnoreCase(o2.getNativeName());
            }
        });
        if(reverseResult){
            Collections.reverse(mCountries);
        }
        updateAdapter(mCountries);
    }
不要忘记将reverseResult boolean声明为全局

您可以使用以下方法:

public void sortByNativeName() {
        Collections.sort(mCountries, new Comparator<Country>() {
            @Override
            public int compare(Country o1, Country o2) {
                return o1.getNativeName().compareToIgnoreCase(o2.getNativeName());
            }
        });
        if(reverseResult){
            Collections.reverse(mCountries);
        }
        updateAdapter(mCountries);
    }
不要忘记将reverseResult boolean声明为全局

public class SampleActivity extends AppCompatActivity {
List<Country> mCountries;
ListView listview;
CustomAdapter adapter;
boolean isAssending = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_admin);
    mCountries = new ArrayList<>();
    listview = findViewById(R.id.listview);

    mCountries.add(new Country("England"));
    mCountries.add(new Country("India"));
    mCountries.add(new Country("France"));
    mCountries.add(new Country("Reunion"));
    mCountries.add(new Country("Nepal"));

    adapter = new CustomAdapter(this, mCountries);
    listview.setAdapter(adapter);

}
public void sortData(View view) {
    if (isAssending) {
        Collections.sort(mCountries, new Comparator<Country>() {
            @Override
            public int compare(Country o1, Country o2) {
                return o2.getNativeName().compareToIgnoreCase(o1.getNativeName());
            }
        });
        adapter.notifyDataSetChanged();
        isAssending=false;
    }
    else {
        Collections.reverse(mCountries);
        adapter.notifyDataSetChanged();
        isAssending=true;
    }
}
class CustomAdapter extends BaseAdapter {
    Context context;
    List<Country> mCountries;

    public CustomAdapter(Context context, List<Country> mCountries) {
        this.context = context;
        this.mCountries = mCountries;
    }

    @Override
    public int getCount() {
        return mCountries.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
        View v = layoutInflater.inflate(R.layout.list_row, viewGroup, false);
        TextView textview = v.findViewById(R.id.textview);
        textview.setText(mCountries.get(i).getNativeName());
        return v;
    }
}
class Country {
    private String nativeName;
    public Country(String nativeName) {
        this.nativeName = nativeName;
    }
    public String getNativeName() {
        return nativeName;
    }
    public void setNativeName(String nativeName) {
        this.nativeName = nativeName;
    }
}}
公共类SampleActivity扩展了AppCompative活动{
列出国家;
列表视图列表视图;
自定义适配器;
布尔值isAssending=true;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin);
mccountries=newarraylist();
listview=findViewById(R.id.listview);
新增(新国家(“英格兰”);
新增(新国家(“印度”);
新增(新国家(“法国”);
添加(新国家(“团聚”);
新增(新国家(“尼泊尔”);
adapter=新的CustomAdapter(此,mccountries);
setAdapter(适配器);
}
公共无效排序数据(视图){
如果(正在发送){
Collections.sort(mccountries,newcomparator(){
@凌驾
公共整数比较(国家o1,国家o2){
返回o2.getNativeName().CompareTIgnoreCase(o1.getNativeName());
}
});
adapter.notifyDataSetChanged();
isAssending=false;
}
否则{
收款。反向(货币单位);
adapter.notifyDataSetChanged();
isAssending=true;
}
}
类CustomAdapter扩展了BaseAdapter{
语境;
列出国家;
公共CustomAdapter(上下文上下文,列表mCountries){
this.context=上下文;
this.mccountries=mccountries;
}
@凌驾
public int getCount(){
返回mccountries.size();
}
@凌驾
公共对象getItem(int i){
返回i;
}
@凌驾
公共长getItemId(int i){
返回i;
}
@凌驾
公共视图getView(int i、视图视图、视图组视图组){
LayoutInflater LayoutInflater=(LayoutInflater)context.getSystemService(布局\充气器\服务);
视图v=布局更平坦。充气(R.layout.list_行,视图组,false);
TextView TextView=v.findViewById(R.id.TextView);
setText(mccountries.get(i.getNativeName());
返回v;
}
}
阶级国家{
私有字符串nativeName;
公共国家/地区(字符串nativeName){
this.nativeName=nativeName;
}
公共字符串getNativeName(){
返回本机名称;
}
公共void setNativeName(字符串nativeName){
this.nativeName=nativeName;
}
}}
试试这个

public class SampleActivity extends AppCompatActivity {
List<Country> mCountries;
ListView listview;
CustomAdapter adapter;
boolean isAssending = true;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_admin);
    mCountries = new ArrayList<>();
    listview = findViewById(R.id.listview);

    mCountries.add(new Country("England"));
    mCountries.add(new Country("India"));
    mCountries.add(new Country("France"));
    mCountries.add(new Country("Reunion"));
    mCountries.add(new Country("Nepal"));

    adapter = new CustomAdapter(this, mCountries);
    listview.setAdapter(adapter);

}
public void sortData(View view) {
    if (isAssending) {
        Collections.sort(mCountries, new Comparator<Country>() {
            @Override
            public int compare(Country o1, Country o2) {
                return o2.getNativeName().compareToIgnoreCase(o1.getNativeName());
            }
        });
        adapter.notifyDataSetChanged();
        isAssending=false;
    }
    else {
        Collections.reverse(mCountries);
        adapter.notifyDataSetChanged();
        isAssending=true;
    }
}
class CustomAdapter extends BaseAdapter {
    Context context;
    List<Country> mCountries;

    public CustomAdapter(Context context, List<Country> mCountries) {
        this.context = context;
        this.mCountries = mCountries;
    }

    @Override
    public int getCount() {
        return mCountries.size();
    }

    @Override
    public Object getItem(int i) {
        return i;
    }

    @Override
    public long getItemId(int i) {
        return i;
    }
    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
        View v = layoutInflater.inflate(R.layout.list_row, viewGroup, false);
        TextView textview = v.findViewById(R.id.textview);
        textview.setText(mCountries.get(i).getNativeName());
        return v;
    }
}
class Country {
    private String nativeName;
    public Country(String nativeName) {
        this.nativeName = nativeName;
    }
    public String getNativeName() {
        return nativeName;
    }
    public void setNativeName(String nativeName) {
        this.nativeName = nativeName;
    }
}}
公共类SampleActivity扩展了AppCompative活动{
列出国家;
列表视图列表视图;
自定义适配器;
布尔值isAssending=true;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_admin);
mccountries=newarraylist();
listview=findViewById(R.id.listview);
新增(新国家(“英格兰”);
新增(新国家(“印度”);
新增(新国家(“法国”);
添加(新国家(“团聚”);
新增(新国家(“尼泊尔”);
adapter=新的CustomAdapter(此,mccountries);
setAdapter(适配器);
}
公共无效排序数据(视图){
如果(正在发送){
Collections.sort(mccountries,newcomparator(){
@凌驾
公共整数比较(国家o1,国家o2){
返回o2.getNativeName().CompareTIgnoreCase(o1.getNativeName());
}
});
adapter.notifyDataSetChanged();
isAssending=false;
}
否则{
收款。反向(货币单位);
adapter.notifyDataSetChanged();
isAssending=true;
}
}
类CustomAdapter扩展了BaseAdapter{
语境;
列出国家;
公共CustomAdapter(上下文上下文,列表mCountries){
this.context=上下文;
this.mccountries=mccountries;
}
@凌驾
public int getCount(){
返回mccountries.size();
}
@凌驾
公共对象getItem(int i){
返回i;
}
@凌驾
公共长getItemId(int i){
返回i;
}
@凌驾
公共视图getView(int i、视图视图、视图组视图组){
LayoutInflater LayoutInflater=(LayoutInflater)context.getSystemService(布局\充气器\服务);
视图v=布局更平坦。充气(R.layout.list_行,视图组,false);
TextView TextView=v.findViewById(R.id.TextView);
setText(mccountries.get(i.getNativeName());
返回v;
}
}
阶级国家{
私有字符串nativeName;
公共国家/地区(字符串nativeName){
this.nativeName=nativeName;
}
公共字符串getNativeName(){
返回本机名称;
}
公共void setNativeName(字符串nativeName){
this.nativeName=nativeName;
}
}}

非常感谢您的支持,这是我们的工作。我怎样才能用双变量进行同样的排序呢?非常感谢你,这是工作。如何使用双变量进行相同的排序?