如何使用ArrayList
按升序对Comparator
进行排序?我知道如何使用以下命令按降序对其进行排序:
Comparator mycomparator = Collections.reverseOrder();
然后
Collections.sort(myarrayList,mycomparator);
只是想知道如何使用集合和比较器按升序对其进行排序?谢谢!
答案 0 :(得分:21)
把它扔到那里......你不能这样做:
Collections.sort(myarrayList);
虽然已经过了一段时间......
答案 1 :(得分:9)
使用默认版本:
Collections.sort(myarrayList);
当然,这要求您的元素实现Comparable
,但对于您提到的版本也是如此。
List<MyClass> list = new ArrayList<MyClass>();
// now fill up the list
// compile error here unless MyClass implements Comparable
Collections.sort(list);
答案 2 :(得分:1)
完成此任务的两种方法:
Collections.sort(myArray)
myArray中的给定元素实现了Comparable
第二
Collections.sort(myArray, new MyArrayElementComparator());
myArray
中的元素MyArrayElementComparator
为Comparator
答案 3 :(得分:1)
这是一个完整的例子:
假设我们有一个Person类,如:
public class Person
{
protected String fname;
protected String lname;
public Person()
{
}
public Person(String fname, String lname)
{
this.fname = fname;
this.lname = lname;
}
public boolean equals(Object objet)
{
if(objet instanceof Person)
{
Person p = (Person) objet;
return (p.getFname().equals(this.fname)) && p.getLname().equals(this.lname));
}
else return super.equals(objet);
}
@Override
public String toString()
{
return "Person(fname : " + getFname + ", lname : " + getLname + ")";
}
/** Getters and Setters **/
}
现在我们创建一个比较器:
import java.util.Comparator;
public class ComparePerson implements Comparator<Person>
{
@Override
public int compare(Person p1, Person p2)
{
if(p1.getFname().equalsIgnoreCase(p2.getFname()))
{
return p1.getLname().compareTo(p2.getLname());
}
return p1.getFname().compareTo(p2.getFname());
}
}
最后假设我们有一群人:
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class Group
{
protected List<Person> listPersons;
public Group()
{
this.listPersons = new ArrayList<Person>();
}
public Group(List<Person> listPersons)
{
this.listPersons = listPersons;
}
public void order(boolean asc)
{
Comparator<Person> comp = asc ? new ComparePerson() : Collections.reverseOrder(new ComparePerson());
Collections.sort(this.listPersons, comp);
}
public void display()
{
for(Person p : this.listPersons)
{
System.out.println(p);
}
}
/** Getters and Setters **/
}
现在我们试试这个:
import java.util.ArrayList;
import java.util.List;
public class App
{
public static void main(String[] args)
{
Group g = new Group();
List listPersons = new ArrayList<Person>();
g.setListPersons(listPersons);
Person p;
p = new Person("A", "B");
listPersons.add(p);
p = new Person("C", "D");
listPersons.add(p);
/** you can add Person as many as you want **/
g.display();
g.order(true);
g.display();
g.order(false);
g.display();
}
}
答案 4 :(得分:1)
按值排序
public Map sortByValue(Map map, final boolean ascending) {
Map result = new LinkedHashMap();
try {
List list = new LinkedList(map.entrySet());
Collections.sort(list, new Comparator() {
@Override
public int compare(Object object1, Object object2) {
if (ascending)
return ((Comparable) ((Map.Entry) (object1)).getValue())
.compareTo(((Map.Entry) (object2)).getValue());
else
return ((Comparable) ((Map.Entry) (object2)).getValue())
.compareTo(((Map.Entry) (object1)).getValue());
}
});
for (Iterator it = list.iterator(); it.hasNext();) {
Map.Entry entry = (Map.Entry) it.next();
result.put(entry.getKey(), entry.getValue());
}
} catch (Exception e) {
Log.e("Error", e.getMessage());
}
return result;
}
答案 5 :(得分:1)
这可能有用吗?
Comparator mycomparator =
Collections.reverseOrder(Collections.reverseOrder());