मेरे पास एक क्लास उपयोगकर्ता है जिसमें एक बूलियन फ़ील्ड है, मैं उपयोगकर्ताओं की एक सूची को सॉर्ट करना चाहता हूं, मैं चाहता हूं कि जिन उपयोगकर्ताओं के पास बुलियन फ़ील्ड है, वे सूची के शीर्ष पर हों और उससे अधिक मैं उन्हें उनके नाम से सॉर्ट करना चाहता हूं।दो मानदंडों की तुलना दो मानदंडों से करें
ArrayList<User> users = new ArrayList<User>();
users.add(new User(1,"user1",false));
users.add(new User(2,"user2",true));
users.add(new User(3,"user3",true));
users.add(new User(4,"user4",false));
Collections.sort(users, new Comparator<User>() {
@Override
public int compare(User u1, User u2) {
//Here i'm lookin for what should i add to compare the two users depending to the boolean field
return u1.name.compareTo(u2.name);
}
});
for(User u : users){
System.out.println(u.name);
}
मैं इस परिणाम प्राप्त करने के उपयोगकर्ताओं क्रमबद्ध करना चाहते हैं: कहना है कि मैं उन के इस संग्रह है देता है:
public class User{
int id;
String name;
boolean myBooleanField;
public User(int id, String name, boolean myBooleanField){
this.id = id;
this.name = name;
this.myBooleanField = myBooleanField;
}
@Override
public boolean equals(Object obj) {
return this.id == ((User) obj).id;
}
}
यहाँ स्पष्ट करने के लिए मैं क्या चाहते हैं एक उदाहरण है: यहाँ मेरी वर्ग है :
user2
user3
user1
user4
मुझे लगता है कि आप यदि किसी और मतलब (u1.myBooleanField && u2.myBooleanField!) – user3578325