Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7913325
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T13:49:01+00:00 2026-06-03T13:49:01+00:00

I have a ListActivity populated by the following Array Object: public class VideoLocation {

  • 0

I have a ListActivity populated by the following Array Object:

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(){

    }

I need to sort the ListActivity based on the distance between the Location and the User.
I don’t have any trouble in sorting the ‘name’ String alphabetically, I use this method:

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

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

but the method can’t be used for ‘double’ data type, which I need for ‘latitude’ and ‘longitude’. Besides there’s no attribute ‘distance’ for the object VideoLocation.

How am I able to sort it out even when I can calculate the distance between the Location and the User?

UPDATE

THIS IS THE FINAL SOLUTION! WORKS LIKE A CHARM

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T13:49:02+00:00Added an answer on June 3, 2026 at 1:49 pm

    Either give VideoLocation a field to hold the distance from the user, calculate that for each of your locations, and then sort with a Comparator that compares the distances, or make a Comparator that computes both distances and compares them.

    The first approach requires an extra field in the VideoLocation which you may not want to have. The second approach does some redundant computation (computing distances something like 2n log n times instead of n times). Take your pick; either will work fine and neither is all that expensive.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have following code: public class ShowActivity extends ListActivity implements OnItemClickListener { /** Called
I have a class defined as public class viewGroups extends ListActivity Somewhere in the
i have a following ListActivity: public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Get Strings
I have a ListView in a ListActivity populated with strings. However, only the text-part
I have the following XML layout for a ListActivity. <?xml version=1.0 encoding=utf-8?> <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android
Currently I have a class that is extending the ListActivity class. I need to
I'm having the following problem: I have a ListActivity, its ListView is composed by
I have a list activity .I have created a array string []name = new
I have a ListActivity that's populated with a SimpleCursorAdapter which pulls the data from
I have a ListView in a ListActivity populated by a database table. Each row

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.