I want to sort and reverse sorting in single button click but i able to did only one, how can i implement this sorting and reverse sorting in consecutive button clicks.
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Comparator<Book> bb=Collections.reverseOrder();
bk=Arrays.asList(books);
Collections.sort(bk,bb);
//Collections.shuffle(bk);
// Collections.sort(bk);
bookListView.invalidateViews();
开发者_如何学JAVA }
});
All your code is doing is sorting the list in reverser order.
Collections.reverseOrder() returns a comparator to reverse.
And you pass bb comparator to Collections.sort method which reverse the collection.
From java doc, Sorts the specified list according to the order induced by the specified comparator.
You should sort it after you reverse it.
Use a Boolean
field whose initial value is true. In click handler of button, compare the boolean field value, if it is true
then do sort and set false
to boolean field. If boolean value is false
then do reverse and set true
to boolean field.
boolean state=true;
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if(state) {
//sort
state=false;
}
else{
//reverse
state=true;
}
}
});
精彩评论