Android-java-如何按对象内的某个值对对象列表进行排序

我试图按对象内的特定值对对象的数组列表进行排序。什么是做这种事情的最佳方法。我应该将Collections.sort()与某种比较器一起使用吗?


我试图用一个变量中包含的浮点值对对象列表进行排序。


编辑:这是我到目前为止:


public class CustomComparator implements Comparator<Marker> {

    @Override

    public int compare(Mark o1, Mark o2) {

        return o1.getDistance().compareTo(o2.getDistance());

    }

}

错误状态:无法在原始类型double上调用compareTo(double)。


是因为比较器不能返回某种类型以外的任何东西吗?


慕姐8265434
浏览 1148回答 3
3回答

撒科打诨

如果您要查找默认排序,则应使用Comparable而不是Comparator。看到这里,这可能会有帮助- 类何时应该是Comparable和/或Comparator?尝试这个 -import java.util.ArrayList;import java.util.Collections;import java.util.List;public class TestSort {&nbsp; &nbsp; public static void main(String args[]){&nbsp; &nbsp; &nbsp; &nbsp; ToSort toSort1 = new ToSort(new Float(3), "3");&nbsp; &nbsp; &nbsp; &nbsp; ToSort toSort2 = new ToSort(new Float(6), "6");&nbsp; &nbsp; &nbsp; &nbsp; ToSort toSort3 = new ToSort(new Float(9), "9");&nbsp; &nbsp; &nbsp; &nbsp; ToSort toSort4 = new ToSort(new Float(1), "1");&nbsp; &nbsp; &nbsp; &nbsp; ToSort toSort5 = new ToSort(new Float(5), "5");&nbsp; &nbsp; &nbsp; &nbsp; ToSort toSort6 = new ToSort(new Float(0), "0");&nbsp; &nbsp; &nbsp; &nbsp; ToSort toSort7 = new ToSort(new Float(3), "3");&nbsp; &nbsp; &nbsp; &nbsp; ToSort toSort8 = new ToSort(new Float(-3), "-3");&nbsp; &nbsp; &nbsp; &nbsp; List<ToSort> sortList = new ArrayList<ToSort>();&nbsp; &nbsp; &nbsp; &nbsp; sortList.add(toSort1);&nbsp; &nbsp; &nbsp; &nbsp; sortList.add(toSort2);&nbsp; &nbsp; &nbsp; &nbsp; sortList.add(toSort3);&nbsp; &nbsp; &nbsp; &nbsp; sortList.add(toSort4);&nbsp; &nbsp; &nbsp; &nbsp; sortList.add(toSort5);&nbsp; &nbsp; &nbsp; &nbsp; sortList.add(toSort6);&nbsp; &nbsp; &nbsp; &nbsp; sortList.add(toSort7);&nbsp; &nbsp; &nbsp; &nbsp; sortList.add(toSort8);&nbsp; &nbsp; &nbsp; &nbsp; Collections.sort(sortList);&nbsp; &nbsp; &nbsp; &nbsp; for(ToSort toSort : sortList){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(toSort.toString());&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}public class ToSort implements Comparable<ToSort> {&nbsp; &nbsp; private Float val;&nbsp; &nbsp; private String id;&nbsp; &nbsp; public ToSort(Float val, String id){&nbsp; &nbsp; &nbsp; &nbsp; this.val = val;&nbsp; &nbsp; &nbsp; &nbsp; this.id = id;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public int compareTo(ToSort f) {&nbsp; &nbsp; &nbsp; &nbsp; if (val.floatValue() > f.val.floatValue()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 1;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if (val.floatValue() <&nbsp; f.val.floatValue()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return -1;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 0;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public String toString(){&nbsp; &nbsp; &nbsp; &nbsp; return this.id;&nbsp; &nbsp; }}

慕尼黑5688855

按照此代码对任何ArrayList进行排序Collections.sort(myList, new Comparator<EmployeeClass>(){&nbsp; &nbsp; public int compare(EmployeeClass obj1, EmployeeClass obj2) {&nbsp; &nbsp; &nbsp; &nbsp; // ## Ascending order&nbsp; &nbsp; &nbsp; &nbsp; return obj1.firstName.compareToIgnoreCase(obj2.firstName); // To compare string values&nbsp; &nbsp; &nbsp; &nbsp; // return Integer.valueOf(obj1.empId).compareTo(Integer.valueOf(obj2.empId)); // To compare integer values&nbsp; &nbsp; &nbsp; &nbsp; // ## Descending order&nbsp; &nbsp; &nbsp; &nbsp; // return obj2.firstName.compareToIgnoreCase(obj1.firstName); // To compare string values&nbsp; &nbsp; &nbsp; &nbsp; // return Integer.valueOf(obj2.empId).compareTo(Integer.valueOf(obj1.empId)); // To compare integer values&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });

HUX布斯

我认为这会更好地帮助您Person p = new Person("Bruce", "Willis");Person p1&nbsp; = new Person("Tom", "Hanks");Person p2 = new Person("Nicolas", "Cage");Person p3 = new Person("John", "Travolta");ArrayList<Person> list = new ArrayList<Person>();list.add(p);list.add(p1);list.add(p2);list.add(p3);Collections.sort(list, new Comparator() {&nbsp; &nbsp; @Override&nbsp; &nbsp; public int compare(Object o1, Object o2) {&nbsp; &nbsp; &nbsp; &nbsp; Person p1 = (Person) o1;&nbsp; &nbsp; &nbsp; &nbsp; Person p2 = (Person) o2;&nbsp; &nbsp; &nbsp; &nbsp; return p1.getFirstName().compareToIgnoreCase(p2.getFirstName());&nbsp; &nbsp; }});
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java
Android