问题
say i have an array list MyArrayList<MYObject>
myObject class looks like :
public class myObject
{
String name;
Int age;
String : city;
}
and my dummy data looks like this :
name : martin , age : 20 , city : NY
name : felix , age : 19 , city : LA
name : brian , age : 21 , city : NY
name : brian , age : 19 , city : NY
now i wanna sort myArraylist (which have the above data in it) in this order -
name : felix , lastname : 19 , city : LA
name : brian , lastname : 21 , city : NY
name : martin , lastname : 20 , city : NY
name : brian , lastname : 19 , city : NY
as you can see that the above data is sorted in two ways - firstly by cities then again by age so this is what i wanna do , i wanna sort an arrayList by an order then again i wanna sort in another order by keeping the first order
anyone knows how can i do it ?? please let me know then
if my question is not clear enough then let me know i'll fix it
回答1:
You can create a Comparator to first compare by city, then by age. Something like this:
Comparator<MyObject> comparator = new Comparator<MyObject>(){
@Override
public int compare(final MyObject o1, final MyObject o2){
if(!o1.getCity().equals(o2.getCity())){
return o1.getCity().compareTo(o2.getCity());
}else{
return o1.getAge().compareTo(o2.getAge());
}
}
};
Collections.sort(myArrayList,comparator);
Edit:
I used the "compareTo" method of the Integer class, which you can't call on an int
primitive type. If you use int
for the age, you could just write out an if statement for the comparison.
回答2:
First of all classes should have a descriptive name and the class names should start with an upper case character. So I'll call your class Person
You need to create a custom Comparator
to be used by the sort utility.
For a reusable solution you could use the Bean Comparator and the Group Comparator.
The
BeanComparator
allows you to sort on any property in the objectThe
GroupComparator
allows you to combine multipleComparator
into a singleComparator
.
So the basic logic would be:
BeanComparator city = new BeanComparator(Person.class, "getCity");
BeanComparator age = new BeanComparator(Person.class, "getAge");
GroupComparator gc = new GroupComparator(city, age);
Collections.sort(arrayList, gc);
回答3:
You have two options, which are essentially equivalent:
- perform two sorts with a stable sort algorithm: first by age, then by city. A stable sort algorithm is a sorting algorithm that maintains the relative order of two elements
A
andB
if they are equivalent under the current<
function. This way, if you first sort by age, when you sort again by city and two elements have the same city, their relative order will be determined by the input order, so they will be sorted by age. - perform a single sort with a compare function that says that
A < B
wheneverA.city != B.city && A.city < B.city
orA.city = B.city && A.age > B.age
.
Notice that Java's Collections.sort
is guaranteed to be stable, so pick whichever strategy better suits your needs.
来源:https://stackoverflow.com/questions/41402963/sort-an-arraylist-with-multiple-conditions