2012-04-27 60 views
0

我有以下數組對象填充的ListActivity:如何使用雙屬性對數組進行排序?

public class VideoLocation { 

    public String deleted_at = null; 
    public int documentary_video_length = -1; 
    public int id = -1; 
    public double latitude = 0d; 
    public double longitude = 0d; 
    public int position = -1; 
    public String updated_at = null; 
    public String name = null; 
    public String text = null; 
    public String documentary_video_url = null; 
    public String documentary_thumbnail_url = null; 
    public String audio_text_url = null; 
    public Footage[] footages = null; 

    public VideoLocation(){ 

    } 

我需要根據位置和用戶之間的距離ListActivity進行排序。 我沒有在按字母順序排序的「名」字符串任何麻煩,我用這個方法:

public void sortAZ(){ 
    Arrays.sort(videoLocations, new Comparator<VideoLocation>() { 

     @Override 
     public int compare(VideoLocation lhs, VideoLocation rhs) { 
      return lhs.name.compareTo(rhs.name); 
     } 
    }); 
} 

,但不能用於「雙」的數據類型,這是我需要的「緯度的方法'和'經度'。此外,對象VideoLocation沒有屬性「距離」。

即使我可以計算位置和用戶之間的距離,我該如何排序呢?

UPDATE

這是最後的解決方案!就像一個魅力

public void sortNearby(){ 
     Arrays.sort(videoLocations, new Comparator<VideoLocation>() { 

      @Override 
      public int compare(VideoLocation lhs, VideoLocation rhs) { 

       double lat = location.getLatitude(); 
       double lng = location.getLongitude(); 

       double lat1 = lhs.latitude; 
       double lng1 = lhs.longitude; 

       double lat2 = rhs.latitude; 
       double lng2 = rhs.longitude; 

       double lhsDistance = countDistance(lat,lng,lat1,lng1); 
       double rhsDistance = countDistance(lat,lng,lat2,lng2); 
       if (lhsDistance < rhsDistance) 
        return -1; 
       else if (lhsDistance > rhsDistance) 
        return 1; 
       else return 0; 
      } 
     }); 
    } 

public double countDistance(double lat1,double lng1, double lat2, double lng2) 
    { 
     Location locationUser = new Location("point A"); 
     Location locationPlace = new Location("point B"); 
     locationUser.setLatitude(lat1); 
     locationUser.setLongitude(lng1); 
     locationPlace.setLatitude(lat2); 
     locationPlace.setLongitude(lng2); 

     double distance = locationUser.distanceTo(locationPlace); 

     return distance; 
    } 

回答

1

要麼給VideoLocation場與一個Comparator的距離比較保存從用戶的距離,計算出每個商家地點,然後排序,或者做一個Comparator,計算兩個距離並比較它們。

第一種方法需要VideoLocation中您可能不想擁有的額外字段。第二種方法做了一些冗餘計算(計算距離大約是2n log n次而不是n次)。拿你的選擇;要麼工作得很好,也不是所有昂貴。排序

@Override 
public int compare(VideoLocation lhs, VideoLocation rhs) { 
    double lhsDistance = ... 
    double rhsDistance = ... 
    if (lhsDistance < rhsDistance) return -1; 
    else if (lhsDistance > rhsDistance) return 1; 
    else return 0; 
} 
+0

我剛剛更新了我的問題,請看看..謝謝 – hectichavana 2012-04-27 14:53:15

+0

正如Igor F.所說,你的新代碼是錯誤的:你需要計算'lhs'和'rhs'的經度和緯度(在你' ),然後調用'countDistance(lat,lng,lat1,lng1)'和'countDistance(lat,lng,lat2,lng2)'(注意:'lat1'),然後調用另一個的緯度和經度。 'lng'1還不存在,但是當你同時計算lats和兩個lng時!)。 – 2012-04-27 15:10:03

+0

我真的不知道它,你能提供一個改進或更正我剛發佈的代碼嗎?這將有所幫助。 Thx – hectichavana 2012-04-27 15:26:24

2

至比較雙打,你可以做到這一點。

+0

我剛剛更新了我的問題,請看看..謝謝 – hectichavana 2012-04-27 14:53:32

+0

thankx你救我的日子 – 2017-03-29 14:16:01

1

或者你可以使用一些功能強大的工具,如Collections ...

ArrayList的someList =新的ArrayList(getDoubleListFromSomewhere());

Collections.sort(someList,新比較<雙>(){

公衆詮釋比較(雙C1,C2雙){

return c1.getName().compareTo(c2.getName()); 

} 

});

之後,你的雙值列表應該全部排序...

Cheerz」

Cehm

1

你的代碼總是返回0,因爲lhsDistance和rhsDistance總是相同的:

double lhsDistance = countDistance(lat,lng,lat2,lng2); 
double rhsDistance = countDistance(lat,lng,lat2,lng2); 

您沒有提供足夠的信息,但我想你想比較用戶和多個不同位置之間的距離,如:

double lhsDistance = countDistance(latUser, lngUser, lat, lng); 
double rhsDistance = countDistance(latUser, lngUser, lat2, lng2); 
+0

你是什麼意思latUser和lngUser?因爲lat和lng實際上屬於用戶 – hectichavana 2012-04-27 15:07:45

+0

如果我理解正確,您正試圖根據它們與一個特殊對象(用戶)的距離來對一組帶有地理標記的對象進行排序。所以(lat,lng)和(lat2,lng2)是來自集合的兩個對象的座標,並且確定它們各自距離的距離(latUser,lngUser)。 – 2012-04-27 15:33:55

+0

謝謝,只是想出了我出錯的地方。我用最終解決方案更新了我的問題 – hectichavana 2012-04-27 15:34:21

相關問題