Sathya Baman
Sathya Baman

Reputation: 3515

Ordering the items in the array

Hi am trying to draw polygons on the map depending on the shortest distance.

i have created the array list called distancearray.

but i want to create a new array list called shortdistance, this array list must have the same values in the distancearray but it must be shorted in assending order depending on the distance.

can any one help me to create this shortdistance array. Thank you.

String array[][]=dbhand.collecting(getIntent().getExtras().getString("temple_type"), Integer.parseInt(getIntent().getExtras().getString("notemples")));

for(int i=0;i<array.length;i++){                
    displaymarkers(Double.parseDouble(array[i][0]), Double.parseDouble(array[i][1]), array[i][2]);
}

for(int i=0;i<array.length;i++){
    double Distance = calculate_distance(SEATTLE_LAT, SEATTLE_LNG, Double.parseDouble(array[i][0]), Double.parseDouble(array[i][1]));

    double[][] distancearray = new double[array.length][3];

    distancearray[i][0]=Double.parseDouble(array[i][0]);
    distancearray[i][1]=Double.parseDouble(array[i][1]);
    distancearray[i][2]=Distance;

}

double [][] shortdistance = new double [array.length][3];

Draw Polygon Function

private void drawpolygon(String array[][]) {

    int lengh = array.length;
    if(lengh==2){
        mMap.addPolygon(new PolygonOptions()
        //.add(new LatLng(9.662502, 80.010239), new LatLng(9.670931, 80.013201), new LatLng(9.663216, 80.01333))
        .add(new LatLng(9.6632139, 80.0133258))
        .add(new LatLng(Double.parseDouble(array[0][0]), Double.parseDouble(array[0][1])))
        .add(new LatLng(Double.parseDouble(array[1][0]), Double.parseDouble(array[1][1])))
       .fillColor(Color.GRAY));
    }

Upvotes: 0

Views: 87

Answers (2)

Jason C
Jason C

Reputation: 40336

You are going to have difficulty sorting your arrays as-is because you currently store your data in parallel arrays:

        distancearray[i][0]=Double.parseDouble(array[i][0]);
        distancearray[i][1]=Double.parseDouble(array[i][2]);
        distancearray[i][3]=Distance;

You can sort distancearray[n] but that will give you nonsense results. Instead what you should do is create a small class that implements Comparable (comparing based on distance) that holds your three values, then work with an array of those classes:

class DistanceInfo implements Comparable<DistanceInfo> {
    public double a;
    public double b;
    public double distance;
    public DistanceInfo (double a, double b, double distance) {
        this.a = a; 
        this.b = b;
        this.distance = distance;
    }
    @Override public int compareTo (DistanceInfo d) { 
        return Double.compare(distance, d.distance);
    }
}

// then:
DistanceInfo[] distancearray = new DistanceInfo[array.length];

// and you can load it using the constructor:
for (int i = 0; i < array.length) {
    double a = Double.parseDouble(array[i][0]);
    double b = Double.parseDouble(array[i][1]);
    distancearray[i] = new DistanceInfo(a, b, Distance);
}

(The fields can be made final if you wish to specify that they cannot be modified after construction.)

Now, Arrays.sort() will sort distancearray based on distance:

Arrays.sort(distancearray, null);

You could also use an ArrayList<DistanceInfo> instead, the idea is the same except you sort with Collections.sort().

In general, for this reason (among others) it is usually better to use a class to store all information about an object as opposed to parallel arrays.


As an aside, you may want to consider using this class for array as well, it will simplify your code a bit (you can modify displaymarkers to take a DistanceInfo, and you can also avoid parsing the same double more than once).

Upvotes: 1

Tim B
Tim B

Reputation: 41188

Just call Arrays.sort() against your array and specify a comparator that looks at the distance.

Upvotes: 4

Related Questions