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;
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.
Thiv
Member
5 Points
45 Posts
Sort and return an array
Jan 25, 2013 10:16 AM|LINK
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;
}
}
goel.ankit
Contributor
2531 Points
513 Posts
Re: Sort and return an array
Jan 25, 2013 11:41 AM|LINK
Can you please give an example of sample data you have and the result you want.
Ankit
(Please select 'Mark as Answer' if my response has helped you.)
Thiv
Member
5 Points
45 Posts
Re: Sort and return an array
Jan 25, 2013 12:13 PM|LINK
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.
gopalanmani
Star
7826 Points
1320 Posts
Re: Sort and return an array
Jan 25, 2013 01:11 PM|LINK
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
hope its help you
Gopalan Mani
My Tech blog
goel.ankit
Contributor
2531 Points
513 Posts
Re: Sort and return an array
Jan 25, 2013 02:19 PM|LINK
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
Ankit
(Please select 'Mark as Answer' if my response has helped you.)
Thiv
Member
5 Points
45 Posts
Re: Sort and return an array
Jan 26, 2013 01:38 AM|LINK
Should be:
In end:
Lat[i] Longi[i] Dist
1.34 103.45 200
1.33 103.77 400
goel.ankit
Contributor
2531 Points
513 Posts
Re: Sort and return an array
Jan 28, 2013 08:03 AM|LINK
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)) { SortedDistance.Add(dist); } 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.
Ankit
(Please select 'Mark as Answer' if my response has helped you.)