Basically its not a Java problem what you need an algo which can provide you the distance between two points represented by the Longitude and Latitude.

The logic would be as follows-

1. Find your longitude and latitude (I hope you already have this) say point A.

2. Find the distance between you and all the products i.e. between A and all the products, say d.

3. Sort all the products based on D

4. You are good to go.

**Formula for the distance**

```
a = sin²(Δφ/2) + cos φ1 ⋅ cos φ2 ⋅ sin²(Δλ/2)
c = 2 ⋅ atan2( √a, √(1−a) )
d = R ⋅ c
```

where φ is latitude, λ is longitude, R is earth’s radius (mean radius = 6,371km);

note that angles need to be in radians to pass to trig functions!

**JavaScript Code**

var R = 6371; // km

var φ1 = lat1.toRadians();

var φ2 = lat2.toRadians();

var Δφ = (lat2-lat1).toRadians();

var Δλ = (lon2-lon1).toRadians();

var a = Math.sin(Δφ/2) * Math.sin(Δφ/2) + Math.cos(φ1) * Math.cos(φ2) * Math.sin(Δλ/2) * Math.sin(Δλ/2);

var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

var d = R * c;

Credit: http://www.movable-type.co.uk/scripts/latlong.html