Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何按数组的双属性排序?_Android - Fatal编程技术网

Android 如何按数组的双属性排序?

Android 如何按数组的双属性排序?,android,Android,我有一个由以下数组对象填充的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 pos

我有一个由以下数组对象填充的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进行排序。 我在按字母顺序排列“name”字符串时没有任何问题,我使用以下方法:

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

        @Override
        public int compare(VideoLocation lhs, VideoLocation rhs) {
            return lhs.name.compareTo(rhs.name);
        }
    });
}
public void sortAZ(){
Arrays.sort(videoLocations,新的比较器(){
@凌驾
公共整数比较(视频位置lhs、视频位置rhs){
返回lhs.name.compareTo(rhs.name);
}
});
}
但是该方法不能用于'double'数据类型,我需要'latitude'和'longitude'数据类型。此外,对象VideoLocation没有属性“距离”

即使我可以计算出位置和用户之间的距离,我如何才能将其分类

更新

这是最终解决方案!工作起来很有魅力

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;
    }
public void sortNearby(){
Arrays.sort(videoLocations,新的比较器(){
@凌驾
公共整数比较(视频位置lhs、视频位置rhs){
双纬度=location.getLatitude();
double lng=location.getLongitude();
双纬度1=lhs纬度;
双lng1=左手经度;
双纬度2=右旋纬度;
双lng2=rhs经度;
双LHS距离=计数距离(lat、lng、lat1、lng1);
双rhsDistance=计数距离(lat、lng、lat2、lng2);
if(左侧距离<右侧距离)
返回-1;
否则如果(左侧距离>右侧距离)
返回1;
否则返回0;
}
});
}
公共双计数距离(双lat1、双lng1、双lat2、双lng2)
{
位置用户=新位置(“点A”);
地点地点=新地点(“B点”);
位置用户设置纬度(lat1);
位置用户设置经度(lng1);
地点设置纬度(纬度2);
地点地点设置经度(lng2);
双距离=locationUser.distanceTo(locationPlace);
返回距离;
}

或者给
视频位置
一个字段来保存与用户的距离,计算每个位置的距离,然后使用比较距离的
比较器
进行排序,或者制作一个计算两个距离并进行比较的
比较器


第一种方法需要在
视频位置
中添加一个您可能不想使用的额外字段。第二种方法进行一些冗余计算(计算距离,例如2n log n次,而不是n次)。你挑吧;这两种方法都可以很好地工作,而且都没有那么昂贵。

要么给
视频定位
一个字段来保存与用户的距离,计算每个位置的距离,然后使用比较距离的
比较器进行排序,要么制作一个计算两个距离并进行比较的
比较器


第一种方法需要在
视频位置
中添加一个您可能不想使用的额外字段。第二种方法进行一些冗余计算(计算距离,例如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;
}
@覆盖
公共整数比较(视频位置lhs、视频位置rhs){
双LHS距离=。。。
双rhsDistance=。。。
if(左侧距离<右侧距离)返回-1;
否则如果(左侧距离>右侧距离)返回1;
否则返回0;
}

要比较双打,可以执行以下操作:

@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;
}
@覆盖
公共整数比较(视频位置lhs、视频位置rhs){
双LHS距离=。。。
双rhsDistance=。。。
if(左侧距离<右侧距离)返回-1;
否则如果(左侧距离>右侧距离)返回1;
否则返回0;
}

您可以使用该方法获取两点与用户之间的距离,然后进行排序。

您可以使用该方法获取两点与用户之间的距离,然后进行排序。

或者您可以使用一些功能强大的工具,如
Collections

ArrayList someList=新的ArrayList(getDoubleListFromSomewhere())

Collections.sort(someList,新的Comparator(){

公共整数比较(双c1,双c2){

}))

之后,双值列表应全部排序

切雷斯


Cehm

或者您可以使用一些功能强大的工具,如
集合

ArrayList someList=新的ArrayList(getDoubleListFromSomewhere())

Collections.sort(someList,新的Comparator(){

公共整数比较(双c1,双c2){

}))

之后,双值列表应全部排序

切雷斯


Cehm

您的代码始终返回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,因为lhsDistance和RHSDSistance始终相同:

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);

你所说的latUser和lngUser是什么意思?因为lat和lng实际上属于用户如果我理解正确的话,你试图根据地理标记对象的集合与一个特殊对象的距离对它们进行排序:用户。所以(lat,lng)和(lat2,lng2)是集合中两个对象的坐标,你确定它们之间的距离(latUser,lngUser)。谢谢你,刚刚找出我的错误。我用最终的解决方案更新了我的问题latUser和lngUser是什么意思?因为