繁体   English   中英

如何在Java或Android中以相反的顺序对对象数组列表进行排序?

[英]How to sort object array list in reverse order in java or android?

我目前有一个这样的数组列表

beaconsList = new Beacon[] {new Beacon("ebefd083-70a2-47c8-9837-e7b5634df520", "park",0),new Beacon("ebefd083-70a2-47c8-9837-e7b5634df521", "shop",0),new Beacon("ebefd083-70a2-47c8-9837-e7b5634df522", "menu",0),new Beacon("ebefd083-70a2-47c8-9837-e7b5634df523", "weather",0),new Beacon("ebefd083-70a2-47c8-9837-e7b5634df524", "video",0)} ;

所以,我创建了一个这样的排序方法,rssi是否是对象的最后一个字段,是否是负整数,我发现结果相反,所以我想知道是否有任何方法可以颠倒比较顺序? 感谢您的帮助

    Arrays.sort(beaconsList, new Comparator<Beacon>() {
            @Override
            public int compare(Beacon lhs, Beacon rhs) {
                 return Integer.compare(lhs.rssi, rhs.rssi);
            }
        });

您可以获取reverseOrder()比较器,然后继续使用Collections.sort(collection, reversedComparator)

或者如果您默认情况下想要相反的顺序,则只需在compare()中交换参数

Integer.compare(lhs.rssi, rhs.rssi);

return Integer.compare(rhs.rssi, lhs.rssi);

您是否尝试过Collection.sort(array,comparator)方法?

请经历这个

beaconsList = new Beacon[] {new Beacon("ebefd083-70a2-47c8-9837-e7b5634df520", "park",0),new Beacon("ebefd083-70a2-47c8-9837-e7b5634df521", "shop",0),new Beacon("ebefd083-70a2-47c8-9837-e7b5634df522", "menu",0),new Beacon("ebefd083-70a2-47c8-9837-e7b5634df523", "weather",0),new Beacon("ebefd083-70a2-47c8-9837-e7b5634df524", "video",0)} ;


Collections.sort(beaconsList, Beacon.property);
I am considering following as attributes of your Beacon class:
i)   String hashCode(eg: "ebefd083-70a2-47c8-9837-e7b5634df520")
ii)  String name (eg: "park")
iii) int id (eg: 0)

Below are two cases which I have considered to provide you the solution:
Case A: When you want to sort the arrays of Beacon based on one attribute, lets say its name. So when we have a requirement to sort elements based on one property, then we user Comparable interface.

so your class should be modified as 

public class Beacon implements Comparable {
String hashCode;
String name;
int id;

// getter and setter of above properties
@overriden
public boolean equals(Object obj) {
// it depends on programmer on which property he wants to check equality for, I am //considering hashCode only, you can check for multiple attributes as well.
// you can check for equals proper implementation in internet

 if(obj instance of Beacon) {
  Beacon beacon = (Beacon) obj ;
  if(this.hashCode.equals(beacon.getHashCode())) {
   return true;
  } else { 
   return false;
  }
 } else {
  // some exception
}
}

@overriden 
public int hashCode() {
  return hashCode.length()+name.lenth()+id;
}
}

Now you have some class where you have the array of Beacon as follow:
Beacon[] beaconsList = new Beacon[] {{new Beacon("ebefd083-70a2-47c8-9837-e7b5634df520", "park",0),new Beacon("ebefd083-70a2-47c8-9837-e7b5634df521", "shop",0),new Beacon("ebefd083-70a2-47c8-9837-e7b5634df522", "menu",0),new Beacon("ebefd083-70a2-47c8-9837-e7b5634df523", "weather",0),new Beacon("ebefd083-70a2-47c8-9837-e7b5634df524", "video",0)} ;

List<Beacon> beaconsColl = Arrays.asList(beaconsList);

// This will give you sorted list based on natural ordering of hashCode property
Collections.sort(beaconsColl); 

Now to reverse it use:

// Below will give you list in sorted list in reverse order of hashCode property.
Collections.reverse(beaconsColl);

Case B: When you have a requirement to sort objects based on multiple properties, in such a case we use Comparator, say we have to sort based on id and hashCode, so we have two create two comparators for it. You can google to see the implementation using comparator. 

I guess solution provided in Case A will the one you were looking for. Comments appreciated. Thanks

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM