簡體   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