need a little help on arrays sorting....

I have a set of data imputed into a set of arrays as the following:

for (int i=0;getline(file,(cities[i]),',');i++)
{
getline(file, countries[i], ',');
getline(file, latitudes[i], ',') ;
getline(file, longitudes[i]);
}

How do I sort the index of "cities[I]" which are the latitudes and longitudes without changing all the elements associated with the that "city[I]" element?

In another word, how do I sort the array line of latitudes and longitudes without changing countries and city it is associated with?
Pack all variables related to single element into struct:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
struct City
{
    std::string name;
    std::string county;
    std::string latitude;
    std::string longitude;
};

bool sortByLatLon(const City& lhs, const City& rhs)
{
    return (lhs.latitude != rhs.latitude) ? (lhs.latitude < rhs.latitude) : 
                                           (lhs.longitude < rhs.longitude);
}

bool sortByName(const City& lhs, const City& rhs)
{
    return lhs.name < rhs.name;
}

//...
City cities[n];
for (int i=0;getline(file,(cities[i].city),','); ++i)
{
    getline(file, cities[i].country, ',');
    getline(file, cities[i].altitude, ',') ;
    getline(file, cities[i].longitude);
}
std::sort(cities, cities + i, sortByName);
Last edited on
I don't really want to use struct is I do not have to, I need to sort latitudes and longitudes of a city that is picked from a cvs file from the user and sort the city's lat and lon from high to low or vice versa to the city the user pick.

but by sorting it high to slow, how to I do it and keep cities and countries along with their lat and long while sorting?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71

#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
#include <sstream>
#include <fstream>
#define _USE_MATH_DEFINES
#include <math.h>


using namespace std;
int main()
{
	//input user for a name on city list
	string input;
	do
	{
	cout << "The worldcities.cvs contained infor on 120 cities." << endl;
	cout << "Enter a city by name or number (1-120)" << endl; 
	std::getline (std::cin,input);
	
	
	const int MAX_ARRAY_SIZE = 121;
	string cities[MAX_ARRAY_SIZE];
	string countries[MAX_ARRAY_SIZE];
	string latitudes[MAX_ARRAY_SIZE];
	string longitudes[MAX_ARRAY_SIZE];	

	

	string temp;


	ifstream file("worldcities.csv");
	if(!file.is_open())
		{
		// file couldn't be opened
		cout << "FAILED: file could not be opened" << endl << "Press enter to close.";
		cin.get();
		return 0;
		getline(file, temp);
		}
	//inputs the file into 4 arrays for each catergories

			for (int i=0;getline(file,(cities[i]),',');i++)
			{ 
			getline(file, countries[i], ',');
			getline(file, latitudes[i], ',') ;
			getline(file, longitudes[i]); 
				
			if(input == cities[i])
					{
						cout << "The city selected is" << " " << cities[i] << latitudes[i] <<endl;
					}
			}		
			



		if(false)
			{
			cout << "I don't understand your input" << endl;
			continue;
			}

	}while (true);

	system("pause");
	return 0;
}
Last edited on
Easy way: use structs and let standard library do their work for you.
Hard way: implement your own sorting algorithm and each time you swap values in one array, swap corresponding values in other arrays.
Topic archived. No new replies allowed.