# Sort and return an array RSS

## 6 replies

Last post Jan 28, 2013 08:03 AM by goel.ankit

Member

5 Points

45 Posts

### Sort and return an array

Hi all, I have a method abcd which returns a value for dist. My question is I am suppose to sort the lat and longi according to the dist which has to be less than 500 and return the sorted lat and longi values. Any idea how to do this? Below is what I have done and I m stuck. Thanks...

for (int k = 0; k < lat.Length; k++)
{
dist = abcd(lat[k], longi[k], usrlatitude, usrlongitude);
SortedDistance[k] = dist;

if(SortedDistance[k] > Convert.ToDouble(500))
{

tempLat = lat[k];
tempLong = longi[k];
lat[k] = lat[k + 1];
longi[k] = longi[k + 1];
lat[k + 1] = tempLat;
longi[k + 1] = tempLong;
}

}

Contributor

2531 Points

513 Posts

### Re: Sort and return an array

Can you please give an example of sample data you have and the result you want.

Regards
Ankit

Member

5 Points

45 Posts

### Re: Sort and return an array

At start:

Lat[i]  Longi[i]   Dist

1.34   103.45     400

1.23   103.45     550

1.33    103.77    200

The method abcd will return the dist. I have to sort the lat[i] and long[i] based on the returned distance which should be less than 500 and return the lat[i] and longi[i] to another method.

In end:

Lat[i]  Longi[i]   Dist

1.34   103.45     200

1.33    103.77    400

Hope this is clearer.

Star

7826 Points

1320 Posts

### Re: Sort and return an array

Hi,

LINQ, that can be used to sort,filter,grouping the collections of objects

check this sample,

http://www.timstall.com/2008/05/using-linq-to-sort-and-filter-entity.html

Regards,
Gopalan Mani
My Tech blog

Contributor

2531 Points

513 Posts

### Re: Sort and return an array

Can you please confirm if Result should be

In end:

Lat[i]  Longi[i]   Dist

1.34   103.45     200

1.33    103.77    400

OR

In end:

Lat[i]  Longi[i]   Dist

1.33    103.77     200

1.34    103.45    400

Regards
Ankit

Member

5 Points

45 Posts

### Re: Sort and return an array

Should be:

In end:

Lat[i]  Longi[i]   Dist

1.34   103.45     200

1.33    103.77    400

Contributor

2531 Points

513 Posts

### Re: Sort and return an array

for convinience sake, I have used List instead of Array which is also better to provide dynamic array functionality.

see if this help ->

```private void Sort()
{
List<double> lat = new List<double>{ 1.34, 1.23, 1.33};
List<double> longi = new List<double>{ 103.45, 103.45, 103.77 };
int dist;

double usrlatitude, usrlongitude;//update values as appropriate

List<int> SortedDistance = new List<int>();

int len = lat.Count;

for (int i = 0; i < len; i++ )
{
if (i < lat.Count)
{
//dist = abcd(lat[i]);
dist = abcd(lat[i], longi[i], usrlatitude, usrlongitude);

if (dist <= Convert.ToDouble(500))
{
}
else
{
lat.RemoveAt(i);
longi.RemoveAt(i);
i--;
}
}
}

int temp;

for (int i = 0; i < SortedDistance.Count; i++)
{
for (int j = i+1; i < SortedDistance.Count; i++)
{
if (SortedDistance[i] > SortedDistance[j])
{
temp = SortedDistance[i];
SortedDistance[i] = SortedDistance[j];
SortedDistance[j] = temp;
}
}
}

for (int i = 0; i < SortedDistance.Count; i++)
{
MessageBox.Show(SortedDistance[i].ToString());
}

for (int i = 0; i < lat.Count; i++)
{
MessageBox.Show(lat[i].ToString());
}

for (int i = 0; i < longi.Count; i++)
{
MessageBox.Show(longi[i].ToString());
}
}```

PS: Sort method can still be optimised.

Regards
Ankit